如何使用自动布局设置UIViews的相等宽度"约束项目格式"

时间:2015-09-09 16:06:25

标签: ios objective-c uiview

在我的应用程序中,我在视图控制器上插入了UIViews。我的主要要求是我想为两个UIViews设置相等的宽度,使用自动布局" Constraint with item format"。

为此,我写了一些代码,但是没有得到相同的宽度。我在这做错了什么?

我想得到如下图像的结果(即需要为两个UIViews设置相等的宽度)。

我的代码:

#import "ViewController8.h"

@interface ViewController8 ()
{
    UIView * myView1;
    UIView * myView2;
}
@end

@implementation ViewController8

- (void)viewDidLoad {
    [super viewDidLoad];

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

    myView2 = [[UIView alloc] init];
    myView2.backgroundColor = [UIColor redColor];
    myView2.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:myView2];

    [self operation2];
}

-(void)operation2
{
    //Applying autolayouts for myview2

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView2
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:0
                                                         multiplier:1.0
                                                           constant:50]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView2
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTrailing
                                                         multiplier:1.0
                                                           constant:-10]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView2
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:-30]];

    //Applying autolayouts for myview1

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView1
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeLeading
                                                         multiplier:1.0
                                                           constant:10]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView1
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:0
                                                         multiplier:1.0
                                                           constant:50]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView1
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:myView2
                                                          attribute:NSLayoutAttributeLeading
                                                         multiplier:1.0
                                                           constant:-30]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView1
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:-30]];
}

enter image description here

3 个答案:

答案 0 :(得分:0)

您发布的代码中没有等宽度约束。尝试添加此

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView1
  attribute:NSLayoutAttributeWidth 
  relatedBy:NSLayoutRelationEqual 
  toItem:nil 
  attribute:NSLayoutAttributeNotAnAttribute 
  multiplier:1.0 
  constant:200]];


[self.view addConstraint:[NSLayoutConstraint constraintWithItem:myView2
  attribute:NSLayoutAttributeWidth 
  relatedBy:NSLayoutRelationEqual 
  toItem:myView1
  attribute:NSLayoutAttributeWidth 
  multiplier:1.0 
  constant:0]];

答案 1 :(得分:0)

添加等宽约束

就足够了
[NSLayoutConstraint constraintWithItem:myView1
                             attribute:NSLayoutAttributeWidth
                             relatedBy:NSLayoutRelationEqual
                                toItem:myView2
                             attribute:NSLayoutAttributeWidth
                            multiplier:1.0
                              constant:0];

不得使用约束明确定义两个视图的宽度,因为view1固定到超视图的前沿,view2固定到后端超级视图和view1的后缘固定在view2的前沿。因此,在添加上述等宽约束后,约束系统完全确定。

添加约束将导致:

simulator screenshot with two equally sized views

答案 2 :(得分:0)

当前解决方案正在使用NSLayoutAnchor

myView1.widthAnchor.constraint(equalTo: myView2.widthAnchor)

不要忘记使用isActive = trueNSLayoutConstraint.activate()激活它。