【问题标题】:UIStoryboard how to replace constraints programmatically?UIStoryboard 如何以编程方式替换约束?
【发布时间】:2013-07-25 19:31:56
【问题描述】:

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

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

-(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 的回答,这是我使用的移除约束的方法

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

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

}

【问题讨论】:

    标签: objective-c ios6 uistoryboard autolayout nslayoutconstraint


    【解决方案1】:

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

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

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

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

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

    【讨论】:

    • 我注意到从父视图中删除子视图似乎不适用于为该子视图定义的约束(如固定高度和宽度)
    • 我回答的第二部分将删除宽度和高度限制。
    • 它有效.. 但我的问题是,如果我的 uiview 在其子视图中有按钮,当我分离然后重新连接其父级时,按钮不再接收触摸事件.. 我不知道如何解决那
    • 好的,这样没有人会和我落入同一个陷阱。我不得不做几件事来找出问题所在(尽管有些与上述问题无关。我认为它仍然可以帮助)1. 无法在我的编译器日志中使用救生recursiveDescription.. 结果我的编译器优化设置是turned on 2. 通过在容器视图上设置translatesAutoresizingMaskIntoConstraints = NO 发现.. 和 设置宽度/高度..>
    • 警告:删除和重新添加视图后,任何引用的 IBOutlets 都将为零。
    猜你喜欢
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 2018-11-25
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多