iOS 7表视图横向单元格宽度

时间:2022-01-02 00:25:46

I've added a table view to a UI View Controller. When I rotate the phone into landscape mode the cell width isn't completely filling the view. See the below screenshot.

我已经为UI视图控制器添加了一个表视图。当我将手机旋转到横向模式时,单元格宽度并未完全填满视图。请参阅以下屏幕截图。

Cannot seem to find an elegant solution here.

似乎无法在这里找到一个优雅的解决方案。

iOS 7表视图横向单元格宽度

EDIT: you can download my example of the landscape view not resizing here:

编辑:您可以下载我的横向视图示例,而不是在此处调整大小:

http://andrewherrick.com/spike/tableviewresize.zip

2 个解决方案

#1


7  

Add the UITableView using auto layout or with the appropriate autoresizing masks.

使用自动布局或相应的自动调整遮罩添加UITableView。

PROGRAMATICALLY

Auto Layout:

UITableView *tableView = [[UITableView alloc] init];
tableview.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:tableView];
NSDictionary *viewsDictionary = @{@"tableView" : tableView };

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|"
                                                                      options:kNilOptions
                                                                      metrics:nil
                                                                        views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|"
                                                                      options:kNilOptions
                                                                      metrics:nil
                                                                        views:viewsDictionary]];

Autoresizing Masks:

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:tableView];

INTERFACE BUILDER / STORYBOARD

界面建筑/故事板

Auto Layout:

Control drag from the table view to the superview and add these constraints:

控制从表视图拖动到超级视图并添加这些约束:

iOS 7表视图横向单元格宽度

Autoresizing Masks (No Auto Layout):

自动调整蒙版(无自动布局):

These should be the autosizing mask for the table view if you are using manual frame manipulation

如果您使用手动帧操作,这些应该是表视图的自动调整掩码

iOS 7表视图横向单元格宽度

NOTE

In all seriousness dude, I have given you as clear away as I can to size your view appropriately to make it flexible. If you struggle to understand this then I suggest you go back and look at a few tutorials for Beginning iOS Development. RayWenderlich is an amazing resource for everything you can think of when starting iOS.

老实说,我尽可能清楚地给你一个适当的视野,使其灵活。如果您很难理解这一点,那么我建议您回过头来看看开始iOS开发的一些教程。 RayWenderlich是启动iOS时所能想到的一切的惊人资源。

UPDATE

Your test project with the added constraints. It literally took me about 5 seconds to do it. Look at the iPhone storyboard.

您的测试项目增加了约束。它花了我大约5秒的时间来完成它。看看iPhone故事板。

http://speedy.sh/2jDwY/tableviewresize.zip

#2


0  

It looks like apple iOS 7 orientation resize table view has some issue.

看起来苹果iOS 7方向调整大小表视图有一些问题。

Using following into right place can resolve problem.

使用以下正确的地方可以解决问题。

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows]
withRowAnimation:UITableViewRowAnimationNone];

Along with this call, if need one may also have to change width as per new orientation size in cellforRowIndexPath data source.

随着此调用,如果需要,可能还必须根据cellforRowIndexPath数据源中的新方向大小更改宽度。

#1


7  

Add the UITableView using auto layout or with the appropriate autoresizing masks.

使用自动布局或相应的自动调整遮罩添加UITableView。

PROGRAMATICALLY

Auto Layout:

UITableView *tableView = [[UITableView alloc] init];
tableview.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:tableView];
NSDictionary *viewsDictionary = @{@"tableView" : tableView };

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tableView]|"
                                                                      options:kNilOptions
                                                                      metrics:nil
                                                                        views:viewsDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tableView]|"
                                                                      options:kNilOptions
                                                                      metrics:nil
                                                                        views:viewsDictionary]];

Autoresizing Masks:

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:tableView];

INTERFACE BUILDER / STORYBOARD

界面建筑/故事板

Auto Layout:

Control drag from the table view to the superview and add these constraints:

控制从表视图拖动到超级视图并添加这些约束:

iOS 7表视图横向单元格宽度

Autoresizing Masks (No Auto Layout):

自动调整蒙版(无自动布局):

These should be the autosizing mask for the table view if you are using manual frame manipulation

如果您使用手动帧操作,这些应该是表视图的自动调整掩码

iOS 7表视图横向单元格宽度

NOTE

In all seriousness dude, I have given you as clear away as I can to size your view appropriately to make it flexible. If you struggle to understand this then I suggest you go back and look at a few tutorials for Beginning iOS Development. RayWenderlich is an amazing resource for everything you can think of when starting iOS.

老实说,我尽可能清楚地给你一个适当的视野,使其灵活。如果您很难理解这一点,那么我建议您回过头来看看开始iOS开发的一些教程。 RayWenderlich是启动iOS时所能想到的一切的惊人资源。

UPDATE

Your test project with the added constraints. It literally took me about 5 seconds to do it. Look at the iPhone storyboard.

您的测试项目增加了约束。它花了我大约5秒的时间来完成它。看看iPhone故事板。

http://speedy.sh/2jDwY/tableviewresize.zip

#2


0  

It looks like apple iOS 7 orientation resize table view has some issue.

看起来苹果iOS 7方向调整大小表视图有一些问题。

Using following into right place can resolve problem.

使用以下正确的地方可以解决问题。

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows]
withRowAnimation:UITableViewRowAnimationNone];

Along with this call, if need one may also have to change width as per new orientation size in cellforRowIndexPath data source.

随着此调用,如果需要,可能还必须根据cellforRowIndexPath数据源中的新方向大小更改宽度。