ios - 更改 Iphone 中的垂直约束

标签 ios objective-c constraints autolayout

我的 nib 文件中有 2 个 View (X 和 Y)。我为此使用了自动布局。

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

如果我从编码中删除 View (Y),那么 View (X) 应该被放置,因为 View ​​(y) 意味着 View (X) 应该向下到 mani View 底部。

我的尝试如下:

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

    [self.view setNeedsLayout];

帮我解决这个问题..

提前致谢...

最佳答案

切勿直接调用 updateConstraints。 而是使用 setNeedsUpdateConstraintsneedsUpdateConstraints

关于问题。

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

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

关于ios - 更改 Iphone 中的垂直约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23293328/

相关文章:

postgresql - 为什么我不能在 PostgreSQL 中设置这个唯一约束?

ios - iOS设备上的Firebase InstanceID token 刷新延迟

r - 如何在 DEoptim (R) 中使用 "fnmap"设置整数约束

mysql - 使用复合键在 GORM 中对现有数据库关系建模

ios - 错误 ITMS-90168 : "The binary you uploaded was invalid."

ios - Realm 默认值属性崩溃

ios - uniqueIdentifier 与 indentifierForVendor 和 Apple 拒绝

ios - 在自定义范围 slider 上设置 alpha (NMRangerSlider)

ios - 为什么 Xcode 4 没有运行我的最新版本?

ios - 访问推送通知列表