在ViewController中的两个视图上以编程方式设置自动布局约束

时间:2016-05-22 07:22:14

标签: objective-c uitableview uiview ios-autolayout

我想在ViewController上添加两个UIView。其中一个是普通的UIView,另一个是UITableView。我不太确定如何设置高度约束,以至于UITableView的内容需要高度,其他视图可以根据UITableView高度自行调整大小。

下面是我用来设置这些约束的简单草图和代码。

enter image description here

 NSDictionary *views = NSDictionaryOfVariableBindings(_infoView,_infoTableView);

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[_infoView]"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:views]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_infoView]-[_infoTableView]"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:views]];

// Width constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.infoView
                                                 attribute:NSLayoutAttributeWidth
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self.view
                                                 attribute:NSLayoutAttributeWidth
                                                multiplier:1.0
                                                  constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.infoTableView
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeWidth
                                                     multiplier:1.0
                                                       constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.infoView
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeTop
                                                     multiplier:1.0
                                                       constant:0]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.infoTableView
                                                      attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0
                                                       constant:0]];

任何意见/反馈都将受到高度赞赏。 感谢。

1 个答案:

答案 0 :(得分:2)

你走在正确的轨道上。这里的诀窍是,您需要动态更新tableView到其contentSize.height的高度,以便infoView知道它应该垂直展开或缩小:

首先定义一个包含高度约束的属性:

@property (nonatomic, strong) NSLayoutConstraint *tableViewHeightConstraint;

- (void)viewDidLoad {
  ...
  self.tableViewHeightConstraint = [NSLayoutConstraint constraintWithItem:self.tableView
                                                                attribute:NSLayoutAttributeHeight
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:nil
                                                                attribute:NSLayoutAttributeNotAnAttribute
                                                               multiplier:0.0
                                                                 constant:350.0];
}

然后你可以在tableView知道它的contentSize 之后立即更新高度约束(例如:viewDidAppear:animated

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];

  self.tableViewHeightConstraint.constant = self.tableView.contentSize.height;
  [self.view layoutIfNeeded];
}  

我为你创建了a sample project,所以你可以玩它来更好地理解我打算做什么。

enter image description here

相关问题