ios AutoLayout问题是否有灵活的宽度?

时间:2013-08-29 06:27:16

标签: ios objective-c autolayout

我认为这是自动布局的问题。

我创建了一个视图,并在其中创建了一些子视图。

内容视图包含子视图,子视图全部放在一行中。

我认为我没有做错什么,但内容视图的框架始终不正确。

像这样:

2013-08-29 14:13:34.688 AutoLayoutBugTest[4658:c07] pV_content.frame {{0, 208}, {319, 44}}

内容视图的宽度是319而不是320,更改viewDidLoad函数中的'cnt'参数,帧可能更差。

像这样的代码:

@interface ViewController ()
@property (nonatomic, strong) UIView            *pV_content;
@property (nonatomic, strong) NSMutableArray    *Arr_views;
@end

@implementation ViewController

- (void)removeConstraintForCustomView:(UIView *)customView
                             fromView:(UIView *)superView
{
    NSMutableArray *pArr_constraint = [NSMutableArray array];
    for (NSLayoutConstraint *constraint in superView.constraints) {
        if ([constraint.firstItem isEqual:customView]) {
            [pArr_constraint addObject:constraint];
        }
    }
    [superView removeConstraints:pArr_constraint];
    pArr_constraint = nil;
}

- (void)constraintCustomView:(UIView *)customView
            matchWidthOfView:(UIView *)superView
                  withHeight:(CGFloat)height
{
    [self removeConstraintForCustomView:customView fromView:superView];

    customView.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *constraint_y = [NSLayoutConstraint constraintWithItem:customView
                                                                      attribute:NSLayoutAttributeCenterY
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:superView
                                                                      attribute:NSLayoutAttributeCenterY
                                                                     multiplier:1.f
                                                                       constant:0.f];
    [superView addConstraint:constraint_y];

    NSLayoutConstraint *constraint_height = [NSLayoutConstraint constraintWithItem:customView
                                                                         attribute:NSLayoutAttributeHeight
                                                                         relatedBy:NSLayoutRelationEqual
                                                                            toItem:superView
                                                                         attribute:NSLayoutAttributeHeight
                                                                        multiplier:0.f
                                                                          constant:height];
    [superView addConstraint:constraint_height];

    NSLayoutConstraint *constraint_left = [NSLayoutConstraint constraintWithItem:customView
                                                                       attribute:NSLayoutAttributeLeft
                                                                       relatedBy:NSLayoutRelationEqual
                                                                          toItem:superView
                                                                       attribute:NSLayoutAttributeLeft
                                                                      multiplier:1.f
                                                                        constant:0.f];
    [superView addConstraint:constraint_left];

    NSLayoutConstraint *constraint_width = [NSLayoutConstraint constraintWithItem:customView
                                                                        attribute:NSLayoutAttributeWidth
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:superView
                                                                        attribute:NSLayoutAttributeWidth
                                                                       multiplier:1.f
                                                                         constant:0.f];
    [superView addConstraint:constraint_width];
}

- (void)constraintCustomView:(UIView *)customView
     matchWidthPercentOfView:(UIView *)superView
                    boxCount:(int)boxCount
                    boxIndex:(int)boxIndex
{
    [self removeConstraintForCustomView:customView fromView:superView];

    customView.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *constraint_top = [NSLayoutConstraint constraintWithItem:customView
                                                                      attribute:NSLayoutAttributeTop
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:superView
                                                                      attribute:NSLayoutAttributeTop
                                                                     multiplier:1.f
                                                                       constant:customView.frame.origin.y];
    [superView addConstraint:constraint_top];
    constraint_top.priority = UILayoutPriorityFittingSizeLevel;

    NSLayoutConstraint *constraint_height = [NSLayoutConstraint constraintWithItem:customView
                                                                         attribute:NSLayoutAttributeHeight
                                                                         relatedBy:NSLayoutRelationEqual
                                                                            toItem:superView
                                                                         attribute:NSLayoutAttributeHeight
                                                                        multiplier:0.f
                                                                          constant:customView.frame.size.height];
    [superView addConstraint:constraint_height];
    constraint_height.priority = UILayoutPriorityFittingSizeLevel;

    if (boxCount == 1) {
        NSLayoutConstraint *constraint_percent_x = [NSLayoutConstraint constraintWithItem:customView
                                                                                attribute:NSLayoutAttributeCenterX
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:superView
                                                                                attribute:NSLayoutAttributeCenterX
                                                                               multiplier:1.f
                                                                                 constant:0.f];
        [superView addConstraint:constraint_percent_x];
        constraint_percent_x.priority = UILayoutPriorityFittingSizeLevel;

        NSLayoutConstraint *constraint_width = [NSLayoutConstraint constraintWithItem:customView
                                                                            attribute:NSLayoutAttributeWidth
                                                                            relatedBy:NSLayoutRelationEqual
                                                                               toItem:superView
                                                                            attribute:NSLayoutAttributeWidth
                                                                           multiplier:1.f
                                                                             constant:-4.f];
        [superView addConstraint:constraint_width];
        constraint_width.priority = UILayoutPriorityFittingSizeLevel;
    }
    else {
        if (boxIndex == 0) {
            NSLayoutConstraint *constraint_left = [NSLayoutConstraint constraintWithItem:customView
                                                                               attribute:NSLayoutAttributeLeft
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:superView
                                                                               attribute:NSLayoutAttributeLeft
                                                                              multiplier:1.f
                                                                                constant:0.f];
            [superView addConstraint:constraint_left];
            constraint_left.priority = UILayoutPriorityFittingSizeLevel;

            NSLayoutConstraint *constraint_width = [NSLayoutConstraint constraintWithItem:customView
                                                                                attribute:NSLayoutAttributeWidth
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:superView
                                                                                attribute:NSLayoutAttributeWidth
                                                                               multiplier:(2.f / (CGFloat)(2 * boxCount + 1))
                                                                                 constant:-2.f];
            [superView addConstraint:constraint_width];
            constraint_width.priority = UILayoutPriorityFittingSizeLevel;
        }
        else if (boxIndex == boxCount - 1) {
            NSLayoutConstraint *constraint_right = [NSLayoutConstraint constraintWithItem:customView
                                                                                attribute:NSLayoutAttributeRight
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:superView
                                                                                attribute:NSLayoutAttributeRight
                                                                               multiplier:1.f
                                                                                 constant:0.f];
            [superView addConstraint:constraint_right];
            constraint_right.priority = UILayoutPriorityFittingSizeLevel;

            NSLayoutConstraint *constraint_width = [NSLayoutConstraint constraintWithItem:customView
                                                                                attribute:NSLayoutAttributeWidth
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:superView
                                                                                attribute:NSLayoutAttributeWidth
                                                                               multiplier:(2.f / (CGFloat)(2 * boxCount + 1))
                                                                                 constant:-2.f];
            [superView addConstraint:constraint_width];
            constraint_width.priority = UILayoutPriorityFittingSizeLevel;
        }
        else {
            NSLayoutConstraint *constraint_percent_x = [NSLayoutConstraint constraintWithItem:customView
                                                                                    attribute:NSLayoutAttributeCenterX
                                                                                    relatedBy:NSLayoutRelationEqual
                                                                                       toItem:superView
                                                                                    attribute:NSLayoutAttributeCenterX
                                                                                   multiplier:(((CGFloat)(2 * boxIndex + 1)) / (CGFloat)boxCount)
                                                                                     constant:0.f];
            [superView addConstraint:constraint_percent_x];
            constraint_percent_x.priority = UILayoutPriorityFittingSizeLevel;

            NSLayoutConstraint *constraint_width = [NSLayoutConstraint constraintWithItem:customView
                                                                                attribute:NSLayoutAttributeWidth
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:superView
                                                                                attribute:NSLayoutAttributeWidth
                                                                               multiplier:(2.f / (CGFloat)(2 * boxCount + 1))
                                                                                 constant:-2.f];
            [superView addConstraint:constraint_width];
            constraint_width.priority = UILayoutPriorityFittingSizeLevel;
        }
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];

    self.pV_content = [[UIView alloc] initWithFrame:CGRectMake(0.f,
                                                               0.f,
                                                               self.view.frame.size.width,
                                                               44.f)];
    self.pV_content.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.pV_content];
    [self constraintCustomView:self.pV_content
                      matchWidthOfView:self.view
                            withHeight:44.f];

    int cnt = 5;

    for (int i = 0; i < cnt; i++) {
        UIView *pV_sub = [[UIView alloc] initWithFrame:CGRectMake(0.f,
                                                                  5.f,
                                                                  30.f,
                                                                  35.f)];
        pV_sub.backgroundColor = [UIColor redColor];
        [self.pV_content addSubview:pV_sub];
        [self constraintCustomView:pV_sub
                   matchWidthPercentOfView:self.pV_content
                                  boxCount:cnt
                                  boxIndex:i];
    }
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    NSLog(@"pV_content.frame %@", NSStringFromCGRect(self.pV_content.frame));
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

@end

1 个答案:

答案 0 :(得分:0)

来自指南:

  

约束的形式为“view1.attr1 view2.attr2 * multiplier + constant”。如果您希望表达的约束没有第二个视图和属性,请使用nil和NSLayoutAttributeNotAnAttribute。

次要:尝试使用

constraintsWithVisualFormat:options:metrics:views:

例如,如果您有一个视图testView:

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[testView]|" options:0 metrics:0 views:@{@"testView":self.testView}]];
在constraintsWithVisualFormat方法中,您可以设置优先级和变体,如:@“H:| - (0 @ 100) - [testView(100)] - (0 @ 100) - |”。此testView具有高优先级,因为未设置优先级参数,但从左侧和右侧大小到superview的2个约束优先级为100,并且您的视图testView不会更改自身帧。 你可以设置这样的格式:[testView(50,100)],你的代码意味着:“testView widht可以是50分或100分。 Read this guide