简体   繁体   English

自动布局约束没有得到尊重

[英]Auto-layout constraints not getting respected

I am programmatically creating constraints such that the view controller view has a background view and a toolbar, with the toolbar at the bottom.我以编程方式创建约束,以便视图控制器视图具有背景视图和工具栏,工具栏位于底部。 I want the background view's height to decrease as the height of the toolbar increases.我希望背景视图的高度随着工具栏高度的增加而降低。 After adding the background view and the toolbar as subviews to the view controller's view, I call (in viewDidLoad) the following method that programmatically sets the constraints.在将背景视图和工具栏作为子视图添加到视图控制器的视图后,我调用(在 viewDidLoad 中)以下以编程方式设置约束的方法。

- (void)configureConstraintsForBackgroundViewAndToolbar {

    [self.backgroundView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.toolbarComponent setTranslatesAutoresizingMaskIntoConstraints:NO];

    UIView *constraintsBackgroundView = self.backgroundView;
    UIView *constraintsToolbar = self.toolbarComponent;

    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(
                                    constraintsBackgroundView, 
                                    constraintsToolbar);
    NSDictionary *metricsDictionary = @{
                                        @"toolbarHeight":[NSNumber numberWithFloat:TOOLBAR_HEIGHT]
                                        };

    [NSLayoutConstraint activateConstraints:
            [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[constraintsBackgroundView]-(0)-|" 
            options:NSLayoutFormatDirectionLeadingToTrailing 
            metrics:metricsDictionary 
            views:viewsDictionary]];

    [NSLayoutConstraint activateConstraints:
            [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[constraintsToolbar]-(0)-|" 
            options:NSLayoutFormatDirectionLeadingToTrailing 
            metrics:metricsDictionary 
            views:viewsDictionary]];

    [NSLayoutConstraint activateConstraints:
            [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[constraintsBackgroundView]-(0)-[constraintsToolbar(>=toolbarHeight)]-(0)-|" 
            options:NSLayoutFormatDirectionLeadingToTrailing 
            metrics:metricsDictionary 
            views:viewsDictionary]];
}

This works fine till the moment I try to update the height of the toolbar using the following method (via an embedded text view).在我尝试使用以下方法(通过嵌入的文本视图)更新工具栏的高度之前,这一切正常。 Is there something wrong that I am doing here?我在这里做错了什么吗? The toolbar height increases but the background view does not shorten in height (I debug that using the SparkInspector).工具栏高度增加但背景视图的高度没有缩短(我使用 SparkInspector 进行调试)。 Seems pretty straightforward that am missing something obvious here...似乎很简单,在这里遗漏了一些明显的东西......

- (void)textViewDidChange:(UITextView *)textView {

    if (textView != self.toolbarTextView) {
        return;
    }

    CGFloat currentHeight = self.bounds.size.height;

    CGSize size = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, textView.frame.size.height)];
    CGFloat determinedHeight = fminf(fmaxf(size.height, TOOLBAR_HEIGHT), MAX_TOOLBAR_HEIGHT);

    [self setBounds:CGRectMake(0, 0, self.bounds.size.width, determinedHeight)];    

    [self setNeedsLayout];
}

Also, if I query the toolbar and the background view for their constraints, the constraints between each other that I have set up initially do not come up?另外,如果我查询工具栏和背景视图的约束,我最初设置的彼此之间的约束没有出现? They do show only for the view controller's view... that doesn't sound right to me either...?它们确实只显示视图控制器的视图……这对我来说也不合适……?

Many thanks...非常感谢...

If you are setting up your layout with constraints it does not make sense to set bounds or frame for your views to update the layout.如果您使用约束设置布局,则为视图设置边界或框架以更新布局是没有意义的。 Instead add constraints in a way that you can edit your layout by changing the constant property of the constraint而是以一种可以通过更改约束的constant属性来编辑布局的方式添加约束

For example:例如:

Set height for your toolbar using a specific constraint and store it in a property使用特定约束设置工具栏的高度并将其存储在属性中

NSLayoutConstraint *toolBarHeightConstraint = [NSLayoutConstraint constraintWithItem:constraintsToolbar
                                                                           attribute:NSLayoutAttributeHeight
                                                                           relatedBy:NSLayoutRelationEqual
                                                                              toItem:self.view
                                                                           attribute:NSLayoutAttributeNotAnAttribute
                                                                          multiplier:1
                                                                            constant:TOOLBAR_HEIGHT];

Then change the height of the toolbar by changing the constant for the constraint.然后通过更改约束的常量来更改工具栏的高度。 like:喜欢:

toolBarHeightConstraint.constant = newHeight;

In your case you will also need to change your vertical constraint to:在您的情况下,您还需要将垂直约束更改为:

[NSLayoutConstraint activateConstraints:
            [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[constraintsBackgroundView]-(0)-[constraintsToolbar]-(0)-|" 
            options:NSLayoutFormatDirectionLeadingToTrailing 
            metrics:metricsDictionary 
            views:viewsDictionary]];

For more information have a look at this answer.有关更多信息,请查看答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM