UIStoryboard如何编程方式替换限制? [英] UIStoryboard how to replace constraints programmatically?

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

问题描述

我在启用自动布局故事板布局视图控制器,并正在寻找一种方式来改变约束条件,允许我的看法旋转到景观和重新排列屏幕上的按钮。当我尝试下面的code,我得到两打无法满足的限制,打破约束......的消息,我真的不能去code。

有没有办法用我编程方式指定的约束动态替换从故事板的限制?我要完全覆盖我在一个故事板定义的按键的布局。

   - (无效)updateViewConstraints
{
    [超级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];
    1 = self.loginButton;
    2 = self.getStartedButton;
    3 = self.takeTourButton;
    *的NSDictionary指标= @ {@高度:@ 50.0,@宽度:@ 100.0,@leftSpacing:@ 110.0};
    的NSDictionary *次= NSDictionaryOfVariableBindings(一,二,三);    [self.view removeConstraints:activeTestLabelConstraints];
    [activeTestLabelConstraints removeAllObjects]    如果(isRotatingToLandscape)
    {        [个体经营registerConstraintsWithVisualFormat:@| - [一(二)] - [二(三)] - [三] - |选项​​:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom指标:指标的观点:观点];
        [个体经营registerConstraintsWithVisualFormat:@V:[一(高度)] - |选项​​:0指标:指标的观点:观点];    }其他
    {        [个体经营registerConstraintsWithVisualFormat:@| -leftSpacing- [一(宽)选项:0指标:指标的观点:观点];
        [个体经营registerConstraintsWithVisualFormat:@【二(宽)选项:0指标:指标的观点:观点];
        [个体经营registerConstraintsWithVisualFormat:@[三(宽)选项:0指标:指标的观点:观点];
        [个体经营registerConstraintsWithVisualFormat:@V:[一(高度)] - [两(75)] - [三(100)] - |选项​​:NSLayoutFormatAlignAllCenterX指标:指标的观点:观点];
    }
}

罗布的回答更新,这里的删除,我使用的约束方法

   - (无效)removeConstraintForView:(UIView的*)viewToModify
{    UIView的* TEMP = viewToModify;
    [温度removeFromSuperview]
    [self.view addSubview:温度];}


解决方案

这听起来像你想只删除一个视图中的所有限制。由于在视图上的约束往往被认为的祖先举行,这不是明显的如何轻松去除所有的约束。但它实际上是pretty容易。

删除视图从视图层次结构中移除中间的看法和它以外的其他意见的任何约束。所以,单从它的父删除您的视图,然后将其添加回:

  //删除中间someView和someView非后裔约束。
UIView的*上海华= someView.superview;
[someView removeFromSuperview]
[上海华addSubview:someView];

如果有中间 someView 任何约束和它的后代,这些限制可能被 someView 举行本身(但不能用 someView )的任何后裔举行。如果你想删除这些限制也可以如此直接做的:

  //删除中间someView及其后代任何剩余的约束。
[someView removeConstraints:[someView约束]];

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];
    }


}

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];

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如何编程方式替换限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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