添加视图以编程方式使用自动布局给人'NSGenericException“,理由是:”无法在视图安装约束 [英] Adding View Programatically With Auto Layout Gives 'NSGenericException', reason: 'Unable to install constraint on view

查看:267
本文介绍了添加视图以编程方式使用自动布局给人'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.

我想这code,看看如何在code工作的限制,但它总是导致异常。在code是:

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

这总是导致异常。我知道上面code只是试图确保preVIEW的底部是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?

更新

生成的例外是:

2013年8月5日16:13:28.889样品code [33553:C07 ***终止应用程序由于未捕获的异常'NSGenericException,理由是:无法安装约束视图。是否从该视图的子树外的约束参考的东西吗?这是非法的。约束:或其可NSLayoutConstraint:0x912c430的UIView:0x8561340.bottom == UILayoutContainerView:0x8257340.bottom - 20>观点:LT;的UIView:0x85774e0;帧=(0 0 320 568);不透明= NO; AUTORESIZE = W + H; autoresizesSubviews = NO;层= LT; 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:终止称为抛出异常
(LLDB)

我在约束增加,所以我pretty确保视图在之前的层次结构添加的子视图。

I have added the subview before adding in the constraint so I am pretty sure the view is in hierarchy.

更新2

我父视图的属性设置为'自动调整子视图在IB。该子视图现在转换成景观长方形当设备开启,但其过于狭窄。我现在需要code做出正确的宽度确定它可能?

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.

您要添加的约束 _ 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]

如果你看到歧义布局,那么你的约束不是完全合格的(这样你会得到联合国predictable行为)。如果添加缺少的限制,你应该能够消除此警告。

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天全站免登陆