如何在 iOS 中使用 swift 重新定位视图?
Posted
技术标签:
【中文标题】如何在 iOS 中使用 swift 重新定位视图?【英文标题】:How to reposition view using swift in iOS? 【发布时间】:2015-03-13 11:16:49 【问题描述】:我正在使用故事板为我的应用程序构建 ui 并使用 swift 并且我准备了以下布局,您可以在屏幕截图中看到。
我想隐藏包含 stop1 详细信息的视图,并在运行时将 stop2 视图替换为 stop1。
我正在使用以下代码来隐藏 stop1 和更新 stop2 的约束
stop1Frame.hidden = true
stop2Frame.updateConstraints()
请帮帮我。
将高度限制设置为 stop1 的 0 有效,但在控制台中显示以下错误。
2015-03-13 17:03:52.738 GroundSpan[5392:1777378] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7a644500 V:[UILabel:0x7a643d50'Please tap on + button to...'(63)]>",
"<NSLayoutConstraint:0x7a64cb80 V:[UIView:0x7a63aaa0(0)]>",
"<NSLayoutConstraint:0x7a64cc10 UILabel:0x7a632e20'Stop1'.top == UIView:0x7a63aaa0.topMargin>",
"<NSLayoutConstraint:0x7a64d2b0 V:[UILabel:0x7a643d50'Please tap on + button to...']-(20)-| (Names: '|':UIView:0x7a63aaa0 )>",
"<NSLayoutConstraint:0x7a64d310 V:[UILabel:0x7a632e20'Stop1']-(NSSpace(8))-[UILabel:0x7a643d50'Please tap on + button to...']>")
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7a644500 V:[UILabel:0x7a643d50'Please tap on + button to...'(63)]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-03-13 17:03:52.739 GroundSpan[5392:1777378] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7a64cb80 V:[UIView:0x7a63aaa0(0)]>",
"<NSLayoutConstraint:0x7a64cc10 UILabel:0x7a632e20'Stop1'.top == UIView:0x7a63aaa0.topMargin>",
"<NSLayoutConstraint:0x7a64d2b0 V:[UILabel:0x7a643d50'Please tap on + button to...']-(20)-| (Names: '|':UIView:0x7a63aaa0 )>",
"<NSLayoutConstraint:0x7a64d310 V:[UILabel:0x7a632e20'Stop1']-(NSSpace(8))-[UILabel:0x7a643d50'Please tap on + button to...']>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7a64d310 V:[UILabel:0x7a632e20'Stop1']-(NSSpace(8))-[UILabel:0x7a643d50'Please tap on + button to...']>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
【问题讨论】:
你的 Swift 代码在哪里? 添加了我用来隐藏和更新约束的快速代码 【参考方案1】:为 stop1 的高度约束创建一个IBOutlet
,以便您可以在代码中访问它。
要隐藏 stop1,请将其高度约束设置为 0,这样 stop2 会向上移动。
@IBOutlet weak var stop1HeightConstraint: NSLayoutConstraint!
...
// in your function:
self.stop1HeightConstraint.constant = 0
UIView.animateWithDuration(0.4, animations:
self.view.layoutIfNeeded()
)
这一切都假定您已正确设置自动布局和约束。
【讨论】:
您的技巧使 stop2 重新定位以代替 stop1,但在控制台中出现错误,请查看已编辑的问题以获取错误日志。 这就是我在回答的最后一句话中的意思:您的约束需要正确设置。我们看不到您如何设置约束。白框应该是 UIView,其中的标签和按钮应该相对于该父视图定位。 我没有看到任何红线或黄线每件事都是完美的蓝色,这意味着所有约束都设置得很好。但仍然收到此消息。以上是关于如何在 iOS 中使用 swift 重新定位视图?的主要内容,如果未能解决你的问题,请参考以下文章
iOS - 如何在不调整大小和重新定位按钮视图的情况下更改按钮图像