[英]Resizing UItableview For Different Devices
我有一个带有简单可滚动表的视图控制器。 它的尺寸为320 x 340,位于显示器的下半部分。
我想根据使用的设备来调整大小。
我正在使用自动版式,并通过以下方式设置高度限制:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableHeightConstraint;
我没有看到任何会影响它的固定约束,并且我的头文件中有此约束(它可以正确识别模拟器中的设备):
#define isiPhone6Plus ([[UIScreen mainScreen] bounds].size.height == 736)?TRUE:FALSE
我在viewDidLoad中有这个:
if(isiPhone6) {
self.tableHeightConstraint.constant = (467);
[self.tableView needsUpdateConstraints];
NSLog(@"Height constraint%f",self.tableHeightConstraint.constant);
}
NSLog显示该值已正确设置,但是表大小未更改。 对于其他设备,我也有类似的代码,但表不会移动。
任何想法都欢迎,我在此站点上看到了其他建议,主要是关于像元高度的建议,似乎并不相关。
覆盖viewdidlayoutsubviews
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.tableHeightConstraint.constant = ([[UIScreen mainScreen] bounds].size.height)/2; // If your table view cover half the screen.
}
当使用自动布局时,我们将使用此方法执行所有与帧相关的更改。 正确设置框架时将调用viewDidLayoutSubViews。
更新约束后,需要调用layoutIfNeeded 。
if(isiPhone6) {
self.tableHeightConstraint.constant = (467);
[self.tableView needsUpdateConstraints];
NSLog(@"Height constraint%f",self.tableHeightConstraint.constant);
[self layoutIfNeeded]; /// Add this
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.