调整不同设备的UItableview的大小

时间:2015-12-13 14:15:35

标签: ios objective-c uitableview constraints

我有一个带有简单可滚动表的视图控制器。它的尺寸为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显示正确设置了值,但表格大小没有变化。我有其他设备的类似代码,但表格不会移动。

欢迎任何想法,我在这个网站上看到了其他建议,主要是针对细胞高度,这看起来并不相关。

2 个答案:

答案 0 :(得分:1)

覆盖 viewdidlayoutsubviews

- (void)viewDidLayoutSubviews
  {
      [super viewDidLayoutSubviews];
      self.tableHeightConstraint.constant = ([[UIScreen mainScreen] bounds].size.height)/2; // If your table view cover half the screen.
  }

使用自动布局时,我们会在此方法中执行所有与帧相关的更改。正确设置帧时调用viewDidLayoutSubViews。

答案 1 :(得分:1)

更新约束后,您需要调用layoutIfNeeded

if(isiPhone6) {
    self.tableHeightConstraint.constant = (467);
    [self.tableView needsUpdateConstraints];
    NSLog(@"Height constraint%f",self.tableHeightConstraint.constant);

    [self layoutIfNeeded];         /// Add this
}