【问题标题】:change the Vertical constraints in Iphone更改 Iphone 中的垂直约束
【发布时间】:2014-04-25 12:34:52
【问题描述】:

我的 nib 文件中有 2 个视图(X 和 Y)。我正在为此使用自动布局。

我的第二个视图(Y)是底部空间是 0 查看意味着它位于主视图的底部。 我的第一个视图 (x) 是底部空间为 0 到 View(Y) 表示 X 和 Y 相互连接。

如果我从编码中删除视图 (Y),则视图 (X) 应放置在视图 (y) 的位置,这意味着视图 (X) 应位于 mani 视图底部。

我的尝试如下:

    [vwOperation2 removeFromSuperview];
    NSLog(@"%@",vwOperation2.constraints);
     vwOperation2.translatesAutoresizingMaskIntoConstraints = NO;
    [vwOperation2 updateConstraints];
      vwOperation1.translatesAutoresizingMaskIntoConstraints = NO;
    [vwOperation1 updateConstraints];

    [self.view setNeedsLayout];

帮我解决这个问题..

提前致谢...

【问题讨论】:

  • 您是否正在删除现在无效的约束/向 X 添加新约束以固定到超级视图?

标签: ios objective-c constraints autolayout


【解决方案1】:

切勿直接致电updateConstraints。 宁可使用setNeedsUpdateConstraintsneedsUpdateConstraints

关于问题。

UIViewupdateConstraints 方法中添加您需要考虑视图状态的约束。例如:

- (void)updateConstraints {
    // You can remove all the constraints here are only some of them
    // You may have some IBOutlets for constraints
    [self removeConstraints:self.constraints];

    if (self.shouldHideX) {        
        // add proper constrains here
        // or modify constants
    } else {
        // add proper constrains here
        // or modify constants
    }
    [super updateConstraints];
}

然后在您的代码触发更改中,您应该执行以下操作:

- (void)doSomethingAwesome {
    self.shouldHideX = // determine that

    [self.view setNeedsUpdateConstraints];
    [self.view setNeedsLayout];

    // if you want to animate that
    [UIView animateWithDuration:0.2
                     animations:^{
                   [self needsUpdateConstraints];
                   [self layoutIfNeeded];
     }              completion:^(BOOL finished){ 
       // here e.g. remove your subview from superview
 }];    
}

【讨论】:

    【解决方案2】:

    可能我的答案会是老式的/不是最佳的,但它有效。

    • 为约束设置两个 IBOutlets(比如 constraintForViewY 和 constraintForViewX)
    • 初始化它们

    constraintForViewY.constant = 0

    constraintForViewX.constant = YView.frame.size.height;

    当你删除 YView 时:

    constraintForViewX.constant = 0;

    (可能有动画)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多