我使用UICollectionView创建了一个用于以标记格式显示兴趣的UI。用户从列表中选择兴趣并将其显示为UICollectionViewCell。每个单元格包含一个UILabel,其兴趣在UIButton旁边,带有“X”以删除兴趣。它工作得很好,但有时在添加或删除兴趣时,其中一些布局不正确,正如我在下图中突出显示的那样:
当我打印UILabel的高度和宽度时,它显示它具有适当的大小(绿线所示的适当宽度),但它被渲染为好像我调用了sizeToFit()(我做之前做的更大) ,正如其他细胞正确显示)。这是我用来布局和调整单元格大小的代码:
class InterestsCollectionViewDelegate: NSObject, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {
var selectedInterests: InterestsList!
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let interestLabel = UILabel()
interestLabel.text = selectedInterests.interests[indexPath.row]
interestLabel.sizeToFit()
interestLabel.frame.size.width += 32
let xButtonWidth = 20;
let newSize = CGSize(width: interestLabel.frame.size.width + CGFloat(xButtonWidth), height: CGFloat(26))
print("newSize for \(String(describing: interestLabel.text)): \(newSize)")
return newSize
}
}
class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var leftMargin = sectionInset.left
var maxY: CGFloat = -1.0
attributes?.forEach { layoutAttribute in
if layoutAttribute.frame.origin.y >= maxY {
leftMargin = sectionInset.left
}
layoutAttribute.frame.origin.x = leftMargin
leftMargin += layoutAttribute.frame.width + minimumInteritemSpacing
maxY = max(layoutAttribute.frame.maxY , maxY)
}
return attributes
}
}
有什么可能导致这种情况的想法?提前谢谢。
编辑: 从控制台添加自动布局警告:
2018-03-28 06:43:44.474788-0700 ClikApp[6091:3425572] [LayoutConstraints] 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.
(
"<NSLayoutConstraint:0x1c0095c70 V:|-(8)-[UICollectionView:0x13c012400] (active, names: EditProfileContentView:0x13bd5b420, '|':EditProfileContentView:0x13bd5b420 )>",
"<NSLayoutConstraint:0x1c0096300 V:|-(0)-[EditProfileContentView] (active, names: EditProfileContentView:0x13bd5b420, EditProfileScrollView:0x13c08b000, '|':EditProfileScrollView:0x13c08b000 )>",
"<NSLayoutConstraint:0x1c00968f0 V:|-(0)-[EditProfileScrollView] (active, names: EditProfileScrollView:0x13c08b000, EditProfileSuperview:0x13bd144e0, '|':EditProfileSuperview:0x13bd144e0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x1c0097de0 h=--- v=--- 'UIView-Encapsulated-Layout-Top' EditProfileSuperview.minY == 0 (active, names: EditProfileSuperview:0x13bd144e0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1c0095c70 V:|-(8)-[UICollectionView:0x13c012400] (active, names: EditProfileContentView:0x13bd5b420, '|':EditProfileContentView:0x13bd5b420 )>
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.
2018-03-28 06:43:44.497594-0700 ClikApp[6091:3425572] [LayoutConstraints] 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.
(
"<NSLayoutConstraint:0x1c0095c70 V:|-(8)-[UICollectionView:0x13c012400] (active, names: EditProfileContentView:0x13bd5b420, '|':EditProfileContentView:0x13bd5b420 )>",
"<NSLayoutConstraint:0x1c0096300 V:|-(0)-[EditProfileContentView] (active, names: EditProfileContentView:0x13bd5b420, EditProfileScrollView:0x13c08b000, '|':EditProfileScrollView:0x13c08b000 )>",
"<NSLayoutConstraint:0x1c00968f0 V:|-(0)-[EditProfileScrollView] (active, names: EditProfileScrollView:0x13c08b000, EditProfileSuperview:0x13bd144e0, '|':EditProfileSuperview:0x13bd144e0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x1c0097de0 h=--- v=--- 'UIView-Encapsulated-Layout-Top' EditProfileSuperview.minY == 0 (active, names: EditProfileSuperview:0x13bd144e0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1c0095c70 V:|-(8)-[UICollectionView:0x13c012400] (active, names: EditProfileContentView:0x13bd5b420, '|':EditProfileContentView:0x13bd5b420 )>
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.
2018-03-28 06:43:44.519629-0700 ClikApp[6091:3425572] [LayoutConstraints] 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.
(
"<NSLayoutConstraint:0x1c0095c70 V:|-(8)-[UICollectionView:0x13c012400] (active, names: EditProfileContentView:0x13bd5b420, '|':EditProfileContentView:0x13bd5b420 )>",
"<NSLayoutConstraint:0x1c0096300 V:|-(0)-[EditProfileContentView] (active, names: EditProfileContentView:0x13bd5b420, EditProfileScrollView:0x13c08b000, '|':EditProfileScrollView:0x13c08b000 )>",
"<NSLayoutConstraint:0x1c00968f0 V:|-(0)-[EditProfileScrollView] (active, names: EditProfileScrollView:0x13c08b000, EditProfileSuperview:0x13bd144e0, '|':EditProfileSuperview:0x13bd144e0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x1c0097de0 h=--- v=--- 'UIView-Encapsulated-Layout-Top' EditProfileSuperview.minY == 0 (active, names: EditProfileSuperview:0x13bd144e0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1c0095c70 V:|-(8)-[UICollectionView:0x13c012400] (active, names: EditProfileContentView:0x13bd5b420, '|':EditProfileContentView:0x13bd5b420 )>
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.
2018-03-28 06:43:44.535276-0700 ClikApp[6091:3425572] [LayoutConstraints] 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.
(
"<NSLayoutConstraint:0x1c0095c70 V:|-(8)-[UICollectionView:0x13c012400] (active, names: EditProfileContentView:0x13bd5b420, '|':EditProfileContentView:0x13bd5b420 )>",
"<NSLayoutConstraint:0x1c0096300 V:|-(0)-[EditProfileContentView] (active, names: EditProfileContentView:0x13bd5b420, EditProfileScrollView:0x13c08b000, '|':EditProfileScrollView:0x13c08b000 )>",
"<NSLayoutConstraint:0x1c00968f0 V:|-(0)-[EditProfileScrollView] (active, names: EditProfileScrollView:0x13c08b000, EditProfileSuperview:0x13bd144e0, '|':EditProfileSuperview:0x13bd144e0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x1c0097de0 h=--- v=--- 'UIView-Encapsulated-Layout-Top' EditProfileSuperview.minY == 0 (active, names: EditProfileSuperview:0x13bd144e0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1c0095c70 V:|-(8)-[UICollectionView:0x13c012400] (active, names: EditProfileContentView:0x13bd5b420, '|':EditProfileContentView:0x13bd5b420 )>
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.
2018-03-28 06:43:44.550334-0700 ClikApp[6091:3425572] [LayoutConstraints] 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.
(
"<NSLayoutConstraint:0x1c0095c70 V:|-(8)-[UICollectionView:0x13c012400] (active, names: EditProfileContentView:0x13bd5b420, '|':EditProfileContentView:0x13bd5b420 )>",
"<NSLayoutConstraint:0x1c0096300 V:|-(0)-[EditProfileContentView] (active, names: EditProfileContentView:0x13bd5b420, EditProfileScrollView:0x13c08b000, '|':EditProfileScrollView:0x13c08b000 )>",
"<NSLayoutConstraint:0x1c00968f0 V:|-(0)-[EditProfileScrollView] (active, names: EditProfileScrollView:0x13c08b000, EditProfileSuperview:0x13bd144e0, '|':EditProfileSuperview:0x13bd144e0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x1c0097de0 h=--- v=--- 'UIView-Encapsulated-Layout-Top' EditProfileSuperview.minY == 0 (active, names: EditProfileSuperview:0x13bd144e0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1c0095c70 V:|-(8)-[UICollectionView:0x13c012400] (active, names: EditProfileContentView:0x13bd5b420, '|':EditProfileContentView:0x13bd5b420 )>
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.
更多截图:
答案 0 :(得分:1)
我想通了,我在UILabel上调用了一个更新函数中的sizeToFit(),这个函数在cellForItemAt函数中调用。有趣的是,看起来像cellForItemAt有时会在sizeForItemAt之前运行,有时候会在。之后运行。
感谢您的回复!