使用自动布局添加视图给出'NSGenericException',原因:'无法在视图上安装约束 [英] Adding View Programatically With Auto Layout Gives 'NSGenericException', reason: 'Unable to install constraint on view

查看:347
本文介绍了使用自动布局添加视图给出'NSGenericException',原因:'无法在视图上安装约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 [self.view addSubview:myView] 添加一个视图作为子视图。这在肖像模式下工作正常。然而,它在景观上根本不起作用。如何以编程方式添加布局约束?

I am adding a view as a subview using [self.view addSubview:myView]. This works fine in portrait mode. However, it doesn't work at all in landscape. How do I add layout constraints programatically?

我的视图目前看起来像是纵向矩形,我需要它在横向模式下看起来像横向矩形。

My view currently looks like portrait rectangle and I need it to look like landscape rectangle in landscape mode.

我试过这个代码看看代码中的约束如何工作,但它总是导致异常。代码是:

I tried this code to see how constraints in code work but it always results in an exception. The code is:

[self.view addSubview:_preView];
NSLayoutConstraint *myConstraint = [NSLayoutConstraint
 constraintWithItem:_preView
 attribute:NSLayoutAttributeBottom
 relatedBy:NSLayoutRelationEqual
 toItem:self.view.superview
 attribute:NSLayoutAttributeBottom
 multiplier:1.0
 constant:-239];
[_preView addConstraint:myConstraint];

这总是导致异常。我知道上面的代码只是试图确保底部的预览是在主视图的底部239px。

This always results in an exception. I know the above code just attempts to ensure that the bottom of preview is 239px above the bottom of main view. But that doesn't work either.

你能帮我排序,这样我可以解决横向问题吗?

Could you help me out with sorting this so that I can resolve the landscape issue?

UPDATE

产生的异常是:

2013-08-05 16:13:28.889示例代码[33553:c07] ***由于未捕获异常NSGenericException终止应用程序,原因:无法在视图上安装约束。约束是否引用来自视图的子树外部的东西?这是非法的。约束:< NSLayoutConstraint:0x912c430 UIView:0x8561340.bottom == UILayoutContainerView:0x8257340.bottom-20> view:< UIView:0x85774e0; frame =(0 0; 320 568); opaque = NO; autoresize = W + H; autoresizesSubviews = NO;层=< CALayer的:0x8577490>>'
***第一掷调用堆栈:
(0x1a04012 0x173be7e 0x1a03deb 0x12ee4a0 0xbb983e 0xbb9a27 0xbb9b76 0xbb9d3b 0xbb9c4d 0x1c0d9 0x11395b3 0x19c3376 0x19c2e06 0x19aaa82 0x19a9f44 0x19a9e1b 0x24027e3 0x2402668 0x67fffc 0x2d3d 0x2c65)
libc ++ abi.dylib:terminate调用抛出异常
(lldb)

UPDATE 2

我将父视图的属性设置为IB中的自动调整子视图。子视图现在转换为横向矩形,当设备转动,但它太窄。我现在需要的代码,以确保其正确的宽度可能?

I set the parent view's property to `Autoresize Subviews' in IB. The subview now converts into landscape rectangle when the device is turned but its too narrow. I now need the code to make sure its of correct width maybe?

推荐答案

几个意见:


  1. 您的约束引用 toItem self.view.superview 。我假设你的意思是 self.view

  1. Your constraint references a toItem of self.view.superview. I assume you meant self.view.

c> _preView ,但您应该将它添加到 self.view (如果您进行上述更改;如果没有,您将使用 self.view.superview )。

You're adding the constraint to _preView, but you should add it to self.view (if you make the above change; if not, you'd use self.view.superview). You always add the constraint to the nearest shared parent.

对于您以编程方式创建的视图,请务必设置 translatesAutoresizingMaskIntoConstraints NO

For the views you're creating programmatically, make sure to set translatesAutoresizingMaskIntoConstraints to NO.

因此:

_preView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_preView];
NSLayoutConstraint *myConstraint = [NSLayoutConstraint constraintWithItem:_preView
                                                                attribute:NSLayoutAttributeBottom
                                                                relatedBy:NSLayoutRelationEqual
                                                                   toItem:self.view
                                                                attribute:NSLayoutAttributeBottom
                                                               multiplier:1.0
                                                                 constant:-239];
[self.view addConstraint:myConstraint];







正在离线聊天,最后两个意见:


Chatting to you offline, two final observations:


  1. 您的约束不明确。以后,您可以通过在调试器中运行应用程序,在应用程序运行时点击暂停按钮(),然后在(lldb)提示符处输入

po [[UIWindow keyWindow] _autolayoutTrace]

如果您看到 AMBIGUOUS LAYOUT 完全合格(因此你会得到不可预测的行为)。

If you see AMBIGUOUS LAYOUT, then your constraints are not fully qualified (and thus you'll get unpredictable behavior). If you add the missing constraints, you should be able to eliminate this warning.

如果您希望对基于约束的视图进行动画处理,可以动画更改<$ 约束的属性,而不是通过自己更改框架属性。例如:

If you want to animate constraint based views, you animate the changing of constant properties of the constraints, not by changing frame properties yourself. For example:

    // create subview

    UIView *subview = [[UIView alloc] init];
    subview.backgroundColor = [UIColor lightGrayColor];
    subview.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:subview];

    // create dictionary for VFL commands

    NSDictionary *views = @{@"subview" : subview, @"superview" : self.view};

    // add horizontal constraints

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

    // set the height of the offscreen subview to be the same as its superview

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subview(==superview)]" options:0 metrics:nil views:views]];

    // set the location of the subview to be just off screen below the current view

    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.view.bounds.size.height];
    [self.view addConstraint:constraint];

    // then in two seconds, animate this subview back on-screen (i.e. change the top constraint `constant` to zero)

    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        constraint.constant = 0.0;
        [UIView animateWithDuration:1.0
                         animations:^{
                             [self.view layoutIfNeeded];
                         }];
    });


这篇关于使用自动布局添加视图给出'NSGenericException',原因:'无法在视图上安装约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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