UIStoryboard 如何以编程方式替换约束?

UIStoryboard how to replace constraints programmatically?(UIStoryboard 如何以编程方式替换约束?)

本文介绍了UIStoryboard 如何以编程方式替换约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在情节提要中布置了一个视图控制器,并启用了自动布局,我正在寻找一种方法来更改约束以允许我的视图旋转为横向并重新排列屏幕上的按钮.当我尝试下面的代码时,我收到大约两打无法满足约束,打破约束......"的消息,我无法真正解码.

I have a view controller laid out in a storyboard with autolayout enabled and am looking for a way to change constraints to allow for my view to rotate into landscape and rearrange buttons on the screen. When I try the code below, I get about two dozen "unable to satisfy constraints, breaking constraint..." messages that I cannot really decode.

有没有办法用我以编程方式指定的约束动态替换情节提要中的约束?我想完全覆盖我在情节提要中定义的按钮布局.

Is there a way to dynamically replace constraints from a storyboard with constraints that I specify programmatically? I want to completely override the layout of the buttons that I defined in a storyboard.

-(void)updateViewConstraints
{
    [super updateViewConstraints];

    self.loginButton.translatesAutoresizingMaskIntoConstraints = NO;
    self.getStartedButton.translatesAutoresizingMaskIntoConstraints = NO;
    self.takeTourButton.translatesAutoresizingMaskIntoConstraints = NO;



    [self.loginButton removeConstraints:self.loginButton.constraints];
    [self.getStartedButton removeConstraints:self.getStartedButton.constraints];
    [self.takeTourButton removeConstraints:self.takeTourButton.constraints];


    one = self.loginButton;
    two = self.getStartedButton;
    three = self.takeTourButton;


    NSDictionary *metrics = @{@"height":@50.0,@"width":@100.0,@"leftSpacing":@110.0};
    NSDictionary *views = NSDictionaryOfVariableBindings(one,two,three);

    [self.view removeConstraints:activeTestLabelConstraints];
    [activeTestLabelConstraints removeAllObjects];

    if(isRotatingToLandscape)
    {

        [self registerConstraintsWithVisualFormat:@"|-[one(two)]-[two(three)]-[three]-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views];
        [self registerConstraintsWithVisualFormat:@"V:[one(height)]-|" options:0 metrics:metrics views:views];

    }else
    {

        [self registerConstraintsWithVisualFormat:@"|-leftSpacing-[one(width)]" options:0 metrics:metrics views:views];
        [self registerConstraintsWithVisualFormat:@"[two(width)]" options:0 metrics:metrics views:views];
        [self registerConstraintsWithVisualFormat:@"[three(width)]" options:0 metrics:metrics views:views];
        [self registerConstraintsWithVisualFormat:@"V:[one(height)]-[two(75)]-[three(100)]-|" options:NSLayoutFormatAlignAllCenterX metrics:metrics views:views];
    }


}

更新了 Rob 的答案,这是我使用的移除约束的方法

Updated with Rob's answer, here's the method to remove constraints that I use

-(void)removeConstraintForView:(UIView*)viewToModify
{

    UIView* temp = viewToModify;
    [temp removeFromSuperview];
    [self.view addSubview:temp];

}

推荐答案

听起来您只想删除视图上的所有约束.由于视图上的约束通常由视图的祖先持有,因此如何轻松删除所有约束并不明显.但其实很简单.

It sounds like you want to just remove all the constraints on a view. Since constraints on a view are often held by an ancestor of the view, it's not obvious how to remove all of the constraints easily. But it is actually pretty easy.

从视图层次结构中移除一个视图会移除该视图与其外部的其他视图之间的任何约束.因此,只需从其父视图中删除您的视图,然后将其添加回来:

Removing a view from the view hierarchy removes any constraints betwixt the view and other views outside of it. So just remove your view from its superview and then add it back:

// Remove constraints betwixt someView and non-descendants of someView.
UIView *superview = someView.superview;
[someView removeFromSuperview];
[superview addSubview:someView];

如果在 someView 及其后代之间存在任何约束,则这些约束可能由 someView 本身持有(但不能由 someView<的任何后代持有/代码>).如果你也想删除这些约束,你可以直接这样做:

If there are any constraints betwixt someView and its descendants, those constraints might be held by someView itself (but cannot be held by any descendants of someView). If you want to remove those constraints also, you can do so directly:

// Remove any remaining constraints betwixt someView and its descendants.
[someView removeConstraints:[someView constraints]];

这篇关于UIStoryboard 如何以编程方式替换约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UIStoryboard 如何以编程方式替换约束?

基础教程推荐