Trouble with one of my constrains

时间:2017-04-08 22:29:29

标签: ios swift constraints

I'm having some troubles with one my constrains in code This the error i'm getting

2017-04-08 18:14:36.798 App[465:89996] 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.   ("<_UILayoutSupportConstraint:0x16eb9f00 V:[_UILayoutGuide:0x16d0cb40(64)]>",
"<_UILayoutSupportConstraint:0x16eb2de0 V:|-(0)-[_UILayoutGuide:0x16d0cb40]   (Names: '|':UIView:0x16d32990 )>",
"<NSLayoutConstraint:0x16d06120 V:[_UILayoutGuide:0x16d0cb40]-(0)-[UITableView:0x17ba6e00]>",
"<NSLayoutConstraint:0x16f45f90 V:|-(63.2)-[UITableView:0x17ba6e00]   (Names: '|':UIView:0x16d32990 )>"  ) Will attempt to recover by breaking constraint 

This is the code that i have for the constrain: Viewdidload

let aka = (self.navigationController?.navigationBar.frame.size.height)! + 19.2
holamama = tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: aka)

//This is out of any methods...
var holamama = NSLayoutConstraint()

In my UIBUtton:

NSLayoutConstraint.activate([holamama])

Other UIButton:

NSLayoutConstraint.deactivate([holamama])

So what can i do? Thanks!

2 个答案:

答案 0 :(得分:1)

由于您尚未指定所需的行为,因此我无法建议更好的方法。但是,我可以帮助解释出现了什么问题。

在这种情况下,错误消息非常有用。它列出了四个约束,表明至少有一个是错误的。

"<_UILayoutSupportConstraint:0x16eb9f00 V:[_UILayoutGuide:0x16d0cb40(64)]>",
"<_UILayoutSupportConstraint:0x16eb2de0 V:|-(0)-[_UILayoutGuide:0x16d0cb40]   (Names: '|':UIView:0x16d32990 )>",

这两个约束类型为_UILayoutSupportConstraint,这意味着在这种情况下您没有创建它们。他们一起定义了topLayoutGuide的位置。因此,他们可能不是问题。

"<NSLayoutConstraint:0x16d06120 V:[_UILayoutGuide:0x16d0cb40]-(0)-[UITableView:0x17ba6e00]>",

此约束表示&#34; UITableView的顶部正在触及_UILayoutGuide的底部。

"<NSLayoutConstraint:0x16f45f90 V:|-(63.2)-[UITableView:0x17ba6e00]   (Names: '|':UIView:0x16d32990 )>"

这是您明确创建的约束。它说&#34; UITableView的顶部比其超级视图顶部低63.2点。

你知道为什么Auto Layout会抱怨吗?您告诉它UITableView的顶部必须触及_UILayoutGuide的底部,屏幕顶部64点以下,并且必须为63.2点在屏幕顶部下方。这两个要求是不相容的,因此是错误的。

答案 1 :(得分:0)

我解决了这个问题: 首先,我连接了我的tableview的顶级约束的IBOutlet

@IBOutlet var constrainTop: NSLayoutConstraint!

然后,当我按下按钮时,会发生这种情况:

let aka = (self.navigationController?.navigationBar.frame.size.height)! + 1.0
constrainTop.constant = aka

当我按下另一个按钮时,会发生这种情况:

constrainTop.constant = 0

我只需要修改tableView的常量