1

我在该视图导航栏中有一个视图(320x480 还需要使其与 iphone5 兼容)右键单击我想显示一个高度和宽度为 140x140 的视图,所以它看起来像正确的与主视图对齐并位于导航栏下方(或者我们可以说是视图顶部)

为此,如果我写下面的代码,它也不能正确显示我的子视图(有按钮)是不可点击的。

在这种情况下,我不必使用 XIB。我必须以编程方式设置它。

[self.containerView addSubview:settingsView];


    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:settingsView
                                                              attribute:NSLayoutAttributeTop
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.containerView
                                                              attribute:NSLayoutAttributeTop
                                                             multiplier:1.0
                                                               constant:0.0]];


    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:settingsView
                                                              attribute:NSLayoutAttributeRight
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.containerView
                                                              attribute:NSLayoutAttributeRight
                                                             multiplier:1.0
                                                               constant:0.0]];

    [self.containerView addConstraints:constraints];

有什么想法吗?怎么了?

4

1 回答 1

1
  1. 您的布局很可能不明确。您只是告诉系统将视图固定在顶部和右侧。它可以通过成为右上角的 1x1pt 小盒子来满足这些约束,也可以通过填充整个超级视图来满足这些限制。因此,Auto Layout 系统还需要知道如何在视图被充分约束之前计算子视图的宽度和高度。

    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:settingsView
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute: 0
                                                         multiplier:1.0
                                                           constant:140.0]];
    
    
    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:settingsView
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:0
                                                         multiplier:1.0
                                                           constant:140.0]];
    
  2. 确保translatesAutoresizingMaskIntoConstraints将子视图的属性设置为NO.

试试看,也许它也会修复你的按钮。

于 2013-07-30T06:40:16.577 回答