以编程方式将“自动布局”约束添加到常量宽度和高度的子视图中

时间:2013-10-15 10:04:22

标签: iphone ios objective-c uiview

我有一个大小为20x40的UIView,它已被添加为ViewController的子视图。较小的UIView可在屏幕上拖动,根据拖动的位置,UIView将重置“X”坐标以固定在屏幕边缘。

但是,我想使用/学习“自动布局”为此子视图添加约束,以在横向和纵向模式下保持相同的宽度和高度。

到目前为止

代码:

self.anotherView = [[UIView alloc] initWithFrame: CGRectMake (0.0,(self.view.frame.size.height/2)-45.0f-self.navigationController.navigationBar.frame.size.height,20.0,45.0f)];
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(dragging:)];
    [self.anotherView addGestureRecognizer:panRecognizer];
    [self.anotherView setBackgroundColor: [UIColor blueColor]];
    self.anotherView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview: self.anotherView];
    [self.view bringSubviewToFront:self.anotherView];

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


    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subview(==20)]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:views]];


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


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

但是,上面的代码给出了以下错误:

Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x8b4ef80 H:|-(0)-[UIView:0x8b4eab0]   (Names: '|':UIView:0x8b4cc80 )>",
    "<NSLayoutConstraint:0x8b4f020 H:[UIView:0x8b4eab0(20)]>",
    "<NSLayoutConstraint:0x8b4f0f0 H:[UIView:0x8b4eab0]-(0)-|   (Names: '|':UIView:0x8b4cc80 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x8aa1d30 h=--& v=--& H:[UIView:0x8b4cc80(320)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x8b4f0f0 H:[UIView:0x8b4eab0]-(0)-|   (Names: '|':UIView:0x8b4cc80 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

我到底错在了什么?此外,我正在使用Storyboard,只有这个视图作为subView添加到ViewController。请在下面找到UI当前的截图。

The blue view is freely draggable across the screen!

1 个答案:

答案 0 :(得分:2)

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subview(==20)]|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:views]];

这就是说你希望子视图宽20点,并且还固定在superview的左右边缘,这是表格单元格的内容视图,因此是320点宽。

如果您只想约束视图的宽度,请不要在VFL字符串中包含超级视图指示符(|),就像使用垂直约束一样,或使用{{3使用一些很好的约束到大小的方法。

要拥有一个可变位置,您需要一个单独的约束,将视图的左边缘固定到其超级视图,此约束的constant将在用户拖动时更新。