gpt4 book ai didi

ios - 以编程方式添加自动布局约束时无法正常工作

转载 作者:可可西里 更新时间:2023-11-01 00:52:01 25 4
gpt4 key购买 nike

这是我的观点

enter image description here

黑色区域是一个 View ,称为containerView,具有以下约束

enter image description here

我想动态地添加一个 uitableView,我这样做了:

 func setConstraintsForTableView(tableView: UITableView){

self.containerView.addSubview(tableView)

let bottomConstraint = tableView.bottomAnchor.constraintEqualToAnchor(self.containerView.bottomAnchor)
let leftConstraint = tableView.leftAnchor.constraintEqualToAnchor(self.containerView.leftAnchor)
let rightConstraint = tableView.rightAnchor.constraintEqualToAnchor(self.containerView.rightAnchor)
let topConstraint = tableView.topAnchor.constraintEqualToAnchor(self.containerView.topAnchor)
self.containerView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activateConstraints([bottomConstraint, leftConstraint, rightConstraint, topConstraint])

self.containerView.layoutIfNeeded()

}

但我在日志中得到了这个:

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:0x7fe8c9c2ddb0 h=--& v=--& UITableView:0x7fe8cd05c200.midX == + 120>",
"<NSLayoutConstraint:0x7fe8c9e97340 UIView:0x7fe8c9e8aeb0.trailingMargin == UIView:0x7fe8cb304490.trailing>",
"<NSLayoutConstraint:0x7fe8c9e97390 UIView:0x7fe8cb304490.leading == UIView:0x7fe8c9e8aeb0.leadingMargin>",
"<NSLayoutConstraint:0x7fe8c9c2cf70 H:|-(0)-[UITableView:0x7fe8cd05c200](LTR) (Names: '|':UIView:0x7fe8cb304490 )>",
"<NSLayoutConstraint:0x7fe8c9c2d0c0 UITableView:0x7fe8cd05c200.right == UIView:0x7fe8cb304490.right>",
"<NSLayoutConstraint:0x7fe8cb104c40 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fe8c9e8aeb0(375)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fe8c9c2d0c0 UITableView:0x7fe8cd05c200.right == UIView:0x7fe8cb304490.right>

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-12-19 22:12:16.201 GrabATable[2419:218311] 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)
(
"<_UILayoutSupportConstraint:0x7fe8cb036c70 V:[_UILayoutGuide:0x7fe8c9e956a0(20)]>",
"<_UILayoutSupportConstraint:0x7fe8cb0365c0 V:|-(0)-[_UILayoutGuide:0x7fe8c9e956a0] (Names: '|':UIView:0x7fe8c9e8aeb0 )>",
"<_UILayoutSupportConstraint:0x7fe8cb0363d0 V:[_UILayoutGuide:0x7fe8c9e96410(0)]>",
"<_UILayoutSupportConstraint:0x7fe8cb018490 _UILayoutGuide:0x7fe8c9e96410.bottom == UIView:0x7fe8c9e8aeb0.bottom>",
"<NSAutoresizingMaskLayoutConstraint:0x7fe8c9c2de50 h=--& v=--& UITableView:0x7fe8cd05c200.midY == + 64>",
"<NSLayoutConstraint:0x7fe8c9e97070 UIImageView:0x7fe8c9e8a160.height == 0.33*UIView:0x7fe8c9e8aeb0.height>",
"<NSLayoutConstraint:0x7fe8c9e97110 V:[_UILayoutGuide:0x7fe8c9e956a0]-(0)-[UIImageView:0x7fe8c9e8a160]>",
"<NSLayoutConstraint:0x7fe8c9e972f0 V:[UIImageView:0x7fe8c9e8a160]-(20)-[UIView:0x7fe8cb304490]>",
"<NSLayoutConstraint:0x7fe8c9e973e0 V:[UIView:0x7fe8cb304490]-(0)-[_UILayoutGuide:0x7fe8c9e96410]>",
"<NSLayoutConstraint:0x7fe8c9c2cc50 UITableView:0x7fe8cd05c200.bottom == UIView:0x7fe8cb304490.bottom>",
"<NSLayoutConstraint:0x7fe8c9c2d210 V:|-(0)-[UITableView:0x7fe8cd05c200] (Names: '|':UIView:0x7fe8cb304490 )>",
"<NSLayoutConstraint:0x7fe8cb104c90 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7fe8c9e8aeb0(667)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fe8c9c2cc50 UITableView:0x7fe8cd05c200.bottom == UIView:0x7fe8cb304490.bottom>

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.

最佳答案

感兴趣的输出:

 "<NSAutoresizingMaskLayoutConstraint:0x7fe8c9c2ddb0 h=--& v=--& UITableView:0x7fe8cd05c200.midX == + 120>",

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fe8c9c2d0c0 UITableView:0x7fe8cd05c200.right == UIView:0x7fe8cb304490.right>

您需要在 tableView 上将 translatesAutoresizingMaskIntoConstraints 设置为 false。查看UIView class Reference了解更多详情。

tableView.translatesAutoresizingMaskIntoConstraints = false

来自 UIView 类引用:

Discussion

If this property’s value is true, the system creates a setof constraints that duplicate the behavior specified by the view’sautoresizing mask. This also lets you modify the view’s size andlocation using the view’s frame, bounds, or center properties,allowing you to create a static, frame-based layout within AutoLayout.

Note that the autoresizing mask constraints fully specify the view’ssize and position; therefore, you cannot add additional constraints tomodify this size or position without introducing conflicts. If youwant to use Auto Layout to dynamically calculate the size and positionof your view, you must set this property to false, and then provide anon ambiguous, nonconflicting set of constraints for the view.

By default, the property is set to true for any view youprogrammatically create. If you add views in Interface Builder, thesystem automatically sets this property to false.

关于ios - 以编程方式添加自动布局约束时无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34375959/

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