ios - 更新约束后,按钮未正确定位在自动布局中

标签 ios swift autolayout

我遇到了自动布局的一个奇怪错误。

我有两个并排的按钮(确认和删除),在同一个 View 中,每个按钮的宽度都是它们 super View 的 50%,在某些情况下(当 test = true 时),我想隐藏确认按钮,所以删除按钮占用整个屏幕的宽度。

这是我的自动布局:

enter image description here

确认按钮:

enter image description here

删除按钮:

enter image description here

比例宽度是父 View 的 50% (1:2)。现在这是我的电话,发生在 viewWillAppear 中:

if test {
    self.confirmButtonWidthConstraint.constant = 0
    self.deleteButtonWidthConstraint.constant = (self.deleteButton.superview?.frame.width)!
}

然而,这样做之后的结果是:

enter image description here

检查 UI 调试器后,我发现奇怪的是,这个删除按钮现在的宽度为……480,起始 x 为 -160。所以我真的不知道这里发生了什么。请帮忙!

enter image description here

最佳答案

我可以建议一个不同的策略吗?

将按钮嵌入到堆栈 View (UIStackView) 中,并将按钮设置为均匀填充。

然后您可以使用 button.isHidden = true 将按钮设置为消失。堆栈 View 将为您优雅地处理布局。

关于ios - 更新约束后,按钮未正确定位在自动布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46409720/

相关文章:

ios - 如何触发对所有自动布局约束的更新?

ios - 如何在 iOS 中将大文件 block 明智地上传到 Azure blob 时减少内存使用量?

iphone - ASIHTTPRequest 多个下载控制,如暂停、恢复

objective-c - 在 box2d/cocos2diphone 中,我怎样才能产生一个冲动,使 b 点远离 a 点

ios - 如何让我的 searchBar TableView 执行 segue

ios - 在signUpInBackgroundWithBlock期间将个人资料图片上传到parse.com时出现问题

ios - 向表中插入行时,“尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行”

ios - Swift 中静态 TableView 中特定单元格的公开指示器

ios - 如何使用 AutoLayout 在一个 UITableView 中设置具有两个原型(prototype)的 UITableViewCell 的 AutoHeight?

ios - 主视图从 ScrollView 中占用顶部空间?