【问题标题】:iOS, Adding Programmatic ConstraintsiOS,添加编程约束
【发布时间】:2015-11-13 20:52:01
【问题描述】:

我正在阅读 iOS 教科书的第 16 章 Big Nerd Ranch,并尝试以编程方式添加约束(Xcode 7.1,iOS),但会导致以下错误:

The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7f96f1e272a0 H:|-(0)-[UIImageView:0x7f96f1e3ba90]   
(Names: '|':UIControl:0x7f96f1e33550 )>
When added to a view, the constraint's items must be descendants of that view (or the view itself). 
This will crash if the constraint needs to be resolved before the view hierarchy is assembled. 
Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.`

BNRDetailViewController.xib 文件是BNRDetailViewController 的视图。这是我尝试在BNRDetailViewController 中使用的代码:

- (void)viewDidLoad
{
[super viewDidLoad];

UIImageView *iv = [[UIImageView alloc] initWithImage:nil];

// The contentMode of the image view in the XIB was aspect fit
iv.contentMode = UIViewContentModeScaleAspectFit;

// Do not produce a translated constraint for this view
iv.translatesAutoresizingMaskIntoConstraints = NO;

// The image view was a subview of the view
[self.view addSubview:iv];

// The image view was pointed to by the imageView property
self.imageView = iv;

NSDictionary *nameMap = @{@"imageView": self.imageView,
                          @"dateLabel": self.dateLabel,
                          @"toolbar": self.toolbar};

// imageView is 0 pts from superview at left and right edges
NSArray *horizontalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[imageView]-0-|" options:0 metrics:nil views:nameMap];

// imageView is 8 pts from dateLabel at its top edge
// and 8 pts from toolbar at its bottom edge
NSArray *verticalConstraints =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:[dateLabel]-[imageView]-[toolbar]" options:0 metrics:nil views:nameMap];

[self.view addConstraints:horizontalConstraints];
[self.view addConstraints:verticalConstraints];
}

【问题讨论】:

    标签: ios objective-c constraints


    【解决方案1】:

    您编写的用于创建和应用约束的代码看起来不错(当我在自己的项目中尝试它时它工作正常)。所以你的问题一定出在其他地方。尝试在调试器中运行,并将错误消息中给出的地址与 self.imageView 和 self.view 的地址进行比较。可能其中一个设置不正确。

    特别是,错误表明“|” (超级视图映射到 uiControl。这不一定是问题,但看起来有点可疑。我通常希望您将子视图添加到 UIView,而不是控件。

    【讨论】:

    • view,我的意思是,代表 iPhone 边框的 .xib 文件中的主框架,在 Identity Inspector, Custom Class 中设置为 UIControl。我需要它能够通过触摸背景来关闭键盘(框架中存在一些文本字段)。
    • UIControl 更改为UIView 没有帮助
    • 我刚刚查看了他们的(Big Nerd Ranch,iOS 教科书)代码,viewDidLoad 也是如此,并且它在某种程度上对他们有用。我可以看到的直接区别是,在他们的 .xib 中,我无法选择控制台上方的视图大小(如 wCompact、hAny),但在我的 .xib 中,我可以,这实际上是问题的根源,因为设置尺寸使其不适用于其他尺寸(这是我的另一个问题,与当前尺寸无关:stackoverflow.com/questions/33662924/…)。
    【解决方案2】:

    好的,我发现这个问题也回答了我的其他未回答的问题 (https://stackoverflow.com/questions/33662924/ios-change-size-of-xib-view-to-wany-hany-xcode-7-1)。在File Inspector, Interface Builder Document 中,我选择了两个选项:Use Auto LayoutUse Size Classes。默认情况下,当我们在 Xcode 7 中创建一个新的 .xib 时,这两个选项都会被选中。取消选中Use Size Classes 解决了当前问题的问题,程序不再崩溃。它还解决了另一个问题,让我可以在不同的模拟器上成功运行应用程序而不会消失视图。

    虽然问题解决了,但我又碰到了一个:Developing universal in Xcode 6 所以,比如说,如果我想要一个通用应用程序,并为 iPhone 和 iPad 指定不同的设置,我仍然不知道该怎么做。如果您按照他们在链接的问题中提出的建议(他们选择了两个选项),您可能会像我一样遇到 Xcode 7 的问题。也许我能想到的唯一解决方案是尝试覆盖 UIViewControllers 和其他方法的 supportedInterfaceOrientations 方法,以根据设备更改行为。但是,在 iOS9 之前,许多可用于控制方向的方法(例如,statusBarOrientation)已被弃用,Apple 现在建议使用 Use Size Classes 选项...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 2015-02-21
      相关资源
      最近更新 更多