Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionViewCell中的UILabel有时大小调整不正确_Ios_Swift_Autolayout_Uicollectionviewcell - Fatal编程技术网

Ios UICollectionViewCell中的UILabel有时大小调整不正确

Ios UICollectionViewCell中的UILabel有时大小调整不正确,ios,swift,autolayout,uicollectionviewcell,Ios,Swift,Autolayout,Uicollectionviewcell,我使用UICollectionView制作了一个用于以标记格式显示兴趣的UI。用户从列表中选择兴趣,并显示为UICollectionViewCell。每个单元格都包含一个UILabel和一个UIButton旁边的兴趣,UIButton带有一个“X”以删除兴趣。它基本上可以正常工作,但有时在添加或删除兴趣时,它们中的一些布局不正确,正如我在下图中突出显示的: 当我打印UILabel的高度和宽度时,它显示它具有适当的大小(绿线显示的适当宽度),但它的渲染方式就像我调用了sizeToFit()(在将

我使用UICollectionView制作了一个用于以标记格式显示兴趣的UI。用户从列表中选择兴趣,并显示为UICollectionViewCell。每个单元格都包含一个UILabel和一个UIButton旁边的兴趣,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.
2018-03-28 06:43:44.474788-0700 ClikApp[6091:3425572][LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2018-03-28 06:43:44.497594-0700 ClikApp[6091:3425572][布局约束]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2018-03-28 06:43:44.519629-0700 ClikApp[6091:3425572][LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2018-03-28 06:43:44.535276-0700 ClikApp[6091:3425572][布局约束]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2018-03-28 06:43:44.550334-0700 ClikApp[6091:3425572][LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
更多屏幕截图:

我明白了,我在更新函数中调用UILabel上的sizeToFit(),该函数在cellForItemAt函数中调用。有趣的是,cellForItemAt有时在sizeForItemAt之前运行,有时在sizeForItemAt之后运行


谢谢你的回复

u是否在情节提要中设置了约束?是的,标签和按钮被约束到单元格的边缘,而label.trailinge==button.leadingedge,按钮具有固定的宽度。很难说到底是什么原因导致了这种情况,但可能会问损坏的标签和按钮有什么不同?我认为这是不同的,因为它是第三行,尝试创建另一种情况,你有3在一行,看看它是否也被破坏。这可能会帮助您缩小范围并找到issueThanks@Eyzuky。我无法找到模式,有时在我添加项目时发生,有时在我删除项目时发生,并且无论订单或行号如何。我正在尝试修复控制台上一些看似无关的自动布局警告(现在尝试学习视觉格式语言)。你觉得这像是一个自动布局问题还是其他问题?你能再贴2-3张截图吗?您还可以添加在控制台中得到的警告