在ViewController中的两个视图上以编程方式设置自动布局约束 [英] Setting auto layout constraints programmatically on two views in ViewController

查看:85
本文介绍了在ViewController中的两个视图上以编程方式设置自动布局约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ViewController上添加两个UIView.其中之一是普通的UIView,另一个是UITableView.我不太确定如何对它们设置高度限制,以使UITableView达到其内容所需的高度,而其他视图可以根据UITableView的高度调整自身大小.

I am trying to add two UIView on ViewController. One of them is plain UIView and other is UITableView. I am not quite sure how to set height constraint on them in such away that UITableView will be of height required for its content and other view can resize itself based on UITableView height.

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

Below is the simple sketch and code I am using for setting up those constraints.

 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]];

任何评论/反馈将不胜感激. 谢谢.

Any comments / feedback would be highly appreciated. Thanks.

推荐答案

您处在正确的轨道上.这里的技巧是,您需要将tableView的高度动态更新为它的contentSize.height,这样infoView会知道它应该垂直扩展还是缩小:

You are on the right track. The trick here is that you need to dynamically update the height of tableView to its contentSize.height so infoView will know it should either be vertically expanded or shrunk:

首先,您将定义一个将保留高度限制的属性:

First you define a property that will hold the height constraint:

@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 (ex:viewDidAppear:animated) 后立即更新高度约束:

Then you can update the height constraint right after tableView knows its contentSize (ex: viewDidAppear:animated):

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

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

我为您创建了一个示例项目,因此您可以玩围绕它来更好地了解我打算做什么.

I created a sample project for you so you can play around with it to have a better understanding about what I intend to do.

这篇关于在ViewController中的两个视图上以编程方式设置自动布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆