- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试实现一个 Collection View ,其中每个单元格都有数量可变的 UIButton 实例,首尾水平放置。
因为按钮的数量是动态的,所以我在运行时实例化按钮并以编程方式添加自动布局约束(第一次尝试这样做)。
这是我的代码:
class MyCustomCell: UICollectionViewCell
{
var buttonTitles:[String]?
var buttons = [UIButton]()
override func didMoveToSuperview()
{
super.didMoveToSuperview()
guard let titles = buttonTitles else {
return
}
for button in buttons {
button.removeFromSuperview()
}
buttons.removeAll()
self.translatesAutoresizingMaskIntoConstraints = false
// FIRST LOOP: CREATE/ADD BUTTONS:
for (index, title) in titles.enumerate(){
let button = UIButton(type: UIButtonType.Custom)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle(title, forState: .Normal)
button.tag = index
addSubview(button)
buttons.append(button)
}
// SECOND LOOP: ADD CONSTRAINTS:
for (index, button) in buttons.enumerate(){
// Vertical Constaints (common to all buttons)
button.addConstraint(
NSLayoutConstraint(
item: self,
attribute: NSLayoutAttribute.TopMargin,
relatedBy: NSLayoutRelation.Equal,
toItem: button,
attribute: NSLayoutAttribute.Top,
multiplier: 1,
constant: 0
)
)
button.addConstraint(
NSLayoutConstraint(
item: self,
attribute: NSLayoutAttribute.BottomMargin,
relatedBy: NSLayoutRelation.Equal,
toItem: button,
attribute: NSLayoutAttribute.Bottom,
multiplier: 1,
constant: 0
)
)
// Horizontal Constriants
if index == 0 {
// First - Constrain left to parent (self):
button.addConstraint(
NSLayoutConstraint(
item: self,
attribute: NSLayoutAttribute.LeftMargin,
relatedBy: NSLayoutRelation.Equal,
toItem: button,
attribute: NSLayoutAttribute.Left,
multiplier: 1,
constant: 0
)
)
}
else {
// Not first - Constrain left to previous button:
let previousButton = self.subviews[index - 1]
button.addConstraint(
NSLayoutConstraint(
item: previousButton,
attribute: NSLayoutAttribute.LeftMargin,
relatedBy: NSLayoutRelation.Equal,
toItem: button,
attribute: NSLayoutAttribute.Left,
multiplier: 1,
constant: 0
)
)
if index == (titles.count - 1) {
// Last: Constrain right
button.addConstraint(
NSLayoutConstraint(
item: self,
attribute: NSLayoutAttribute.RightMargin,
relatedBy: NSLayoutRelation.Equal,
toItem: button,
attribute: NSLayoutAttribute.Right,
multiplier: 1,
constant: 0
)
)
}
}
}
}
}
当我尝试添加第一个约束时抛出此异常:
The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7f9702d19610 MyAppName.MyCustomCell:0x7f9703a690e0.topMargin == UIView:0x7f9702d163e0.top>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
2015-10-06 18:39:58.863 Meeting Notes[15219:1817298] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x7f9702d19610 MyAppName.MyCustomCell:0x7f9703a690e0.topMargin == UIView:0x7f9702d163e0.top>
Container hierarchy:
<UIView: 0x7f9702d163e0; frame = (0 0; 97 60); gestureRecognizers = <NSArray: 0x7f96fb64b5c0>; layer = <CALayer: 0x7f96fb606880>>
View not found in container hierarchy: <MyAppName.MyCustomCell: 0x7f9703a690e0; baseClass = UICollectionViewCell; frame = (103 0; 97 60); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7f96fb64bcd0>>
That view's superview: <UICollectionView: 0x7f96fb8cc800; frame = (0 172; 1024 596); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7f9701f813f0>; layer = <CALayer: 0x7f9701f865c0>; contentOffset: {0, 0}; contentSize: {1024, 182}> collection view layout: <UICollectionViewFlowLayout: 0x7f9701f8df50>
(lldb)
讨论 here和其他类似的问题,当约束中涉及的 View 不是父子关系时,就会出现这种情况;但是,在添加任何约束之前我已经添加了所有 subview (我将循环分成两部分)。此外,整个代码都在单元格的 didMovetoSuperview
中运行,因此单元格本身并不是孤立的...
我错过了什么?
最佳答案
查看错误信息:
When added to a view, the constraint's items must be descendants of that view (or the view itself)
在您的情况下,所有约束都应添加到 self
:
self.addConstraint( ...
关于ios - 即使在添加 subview 后, View 层次结构也未准备好约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32967129/
我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:
到目前为止,我已经在Google BigQuery上训练了几种模型,目前我需要查看模型的外观(即架构,损失函数等)。 有没有办法获取这些信息? 最佳答案 仔细阅读文档后,我可以说该功能尚不存在。我什至
本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下: 前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个
我是一名优秀的程序员,十分优秀!