如何使用NSLayoutConstraint将宽度等同于某些UIView? [英] How to pin Widths Equally to some UIViews with NSLayoutConstraint?

查看:80
本文介绍了如何使用NSLayoutConstraint将宽度等同于某些UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用NSLayoutConstraints固定两个以上具有宽度的UIView?

How can I pin more than two UIViews with Widths Equally using NSLayoutConstraints?

现在,我正在使用以下代码,并且无法固定两个以上的UIView:

Right now, I'm using the following code and I can't pin more than two UIViews:

for (int i = 0; i < column.count; i++) {
    NSString *horizontalFormat = @"H:|[view1][view2(==view1)]|";
    NSDictionary *views;
    if (i < column.count - 1) {
        views  = @{
                   @"view1": column[i],
                   @"view2": column[i + 1]
                   };
    }else{
        views  = @{
                   @"view1": column[i - 1],
                   @"view2": column[i]
                   };
    }
    NSArray * horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:horizontalFormat
                                                                    options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom
                                                                    metrics:nil
                                                                      views:views];
    [self.contentView addConstraints:horizontalConstraints];
}

有什么想法吗?

推荐答案

下面是一个示例.所有视图都是在代码中生成的,因此只需将此代码直接复制到UIViewController中(例如复制到其viewDidLoad中)并运行它即可:

Here's an example. All views are generated in the code, so just copy this code right into a UIViewController (e.g. into its viewDidLoad) and run it:

UIView* v1 = [[UIView alloc] init];
v1.layer.borderWidth = 2;
v1.layer.borderColor = [UIColor redColor].CGColor;
v1.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:v1];
[NSLayoutConstraint activateConstraints:
 @[[v1.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:100],
   [v1.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
   [v1.heightAnchor constraintEqualToConstant:40],
   ]];
NSInteger n = 6; // change this number as desired
NSMutableArray* marr = [NSMutableArray new];
[marr addObject:v1];
for (NSInteger i = 1; i < n; i++) {
    UIView* v = [[UIView alloc] init];
    v.layer.borderWidth = 2;
    v.layer.borderColor = [UIColor redColor].CGColor;
    v.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:v];
    [marr addObject:v];
}
for (NSInteger i = 1; i < n; i++) {
    UIView* v = marr[i];
    UIView* prev = marr[i-1];
    [NSLayoutConstraint activateConstraints:
     @[[v.topAnchor constraintEqualToAnchor:v1.topAnchor],
       [v.bottomAnchor constraintEqualToAnchor:v1.bottomAnchor],
       [v.leadingAnchor constraintEqualToAnchor:prev.trailingAnchor],
       [v.widthAnchor constraintEqualToAnchor:v1.widthAnchor]
       ]];
}
UIView* v = marr[n-1];
[NSLayoutConstraint activateConstraints:
 @[[v.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor]
  ]];

这篇关于如何使用NSLayoutConstraint将宽度等同于某些UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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