【问题标题】:Adding constraints programmatically to topLayoutGuide?以编程方式向 topLayoutGuide 添加约束?
【发布时间】:2016-06-03 17:50:07
【问题描述】:

如果我在 viewController 的 viewDidLoad() 中将 UITableView 约束到它的超级视图,一切都会完美运行。

但是,如果我尝试像这样将 tableView 约束到 layoutGuide 的顶部:

 // tableView.top = view.top
myConstraint =[NSLayoutConstraint
               constraintWithItem:theTableView
               attribute:NSLayoutAttributeTop
               relatedBy:NSLayoutRelationEqual
               toItem:self.view
               attribute:NSLayoutAttributeTop
               multiplier:1.0
               constant:0];
myConstraint.identifier = @"tableView.top = layoutguide.bottom";
[self.view addConstraint:myConstraint];

我得到这个错误:

    [UIViewController topLayoutGuide]: guide not available before the view controller's view is loaded
2016-06-03 10:48:06.310 BlinkPDF[304:34590] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specified in pairs'

我尝试将我的所有约束移动到 viewController 的 -(void) updateConstraints 方法中,这消除了运行时错误并正确呈现了视图,但是,当我旋转设备时,视图没有正确更新。

谁能告诉我写那个约束的正确位置?

谢谢!

【问题讨论】:

    标签: objective-c uitableview autolayout


    【解决方案1】:
    Try to call your updateConstraints to viewDidAppear and add this line before calling method 
    
        tableView.translatesAutoresizingMaskIntoConstraints = NO;
    
    So your viewDidAppear method will be like
    
        - (void)viewDidAppear:(BOOL)animated{
           [super viewDidAppear:animated];
           [self updateConstraints];
        }
    

    这会对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 1970-01-01
      相关资源
      最近更新 更多