2013-10-15 52 views
0

我有一个尺寸为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,并且只有这个视图才会作为子视图添加到ViewController中。请在下面查看目前用户界面的截图。

The blue view is freely draggable across the screen!

回答

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

这是说,你想子视图是20点宽,也寄托了上海华盈,这是一个表格单元格的内容视图的左右边缘,因此320点宽。

如果你只是想约束视图的宽度,不包括在VFL串SuperView把指标(|),因为你已经与垂直方向制约进行,或者使用与autolayout helper category一些不错的constrain-以适应规模的方法。

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

+0

谢谢你指点我正确的方向。虽然我喜欢自动布局帮助类,但我会避免使用它,直到我学到一些关于自动布局的东西。 我得到了最初的布局工作(感谢您的回答)现在需要在拖动部分工作。 – iSee

相关问题