gpt4 book ai didi

ios - 以编程方式设置约束导致 Swift 中的冲突

转载 作者:行者123 更新时间:2023-12-01 20:16:05 25 4
gpt4 key购买 nike

正如我在 this GitHub issue 中发布的那样,当我尝试添加自己的约束时,我遇到了一些隐藏的约束,这些约束会引发错误

2016-04-18 19:22:04.270 Metric Time[1519:447809] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0x15e670e70 h=--& v=--& Metric_Time.View:0x15e692e70.midX == + 115>",
"<NSLayoutConstraint:0x15e5048c0 Metric_Time.View:0x15e692e70.centerX == UIView:0x15e6953e0.centerX>",
"<NSAutoresizingMaskLayoutConstraint:0x15e55fd30 h=-&- v=-&- 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UIView:0x15e6953e0] (Names: '|':UIWindow:0x15e688ed0 )>",
"<NSLayoutConstraint:0x15e54e750 'UIView-Encapsulated-Layout-Width' H:[UIView:0x15e6953e0(320)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x15e5048c0 Metric_Time.View:0x15e692e70.centerX == UIView:0x15e6953e0.centerX>

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.`
我已经尝试寻找可以创建这些约束的地方,但搜索我的 ViewController.swift诸如约束、布局等关键字的文件只显示了我编写的代码。
我的代码都在 GitHub如果有人想弄乱它。

最佳答案

如果要以编程方式添加约束,则需要关闭 translateAutoresizingMaskIntoConstraints。您也没有足够的约束来满足布局引擎。请记住,UIView 没有固有大小,如果您在 View 中使用自动布局,则需要使用约束设置所有内容,而不是依赖约束和框架的混合。一种可能的解决方案是添加一些代码,例如:

        self.view.addSubview(clockView)
clockView.translatesAutoresizingMaskIntoConstraints = false
clockView.centerYAnchor.constraintEqualToAnchor(self.view.centerYAnchor, constant: 0.0).active = true
clockView.centerXAnchor.constraintEqualToAnchor(self.view.centerXAnchor, constant: 0.0).active = true
clockView.widthAnchor.constraintEqualToConstant(230.0).active = true
clockView.heightAnchor.constraintEqualToConstant(230.0).active = true

然而,这并不是您想要的,因为当它处于横向时,您的时钟会覆盖您的标签。但是你可以通过使用 clockView.centerYAnchor.constraintEqualToAnchor(self.view.centerYAnchor, constant: 0.0).active = true 来解决这个问题。

关于ios - 以编程方式设置约束导致 Swift 中的冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36707620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com