UICollectionView scrollToItem打破了约束,不会滚动

时间:2018-10-08 07:42:00

标签: ios swift uicollectionview interface-builder

首先,这是我收到的错误消息:

2018-10-08 01:10:04.886509-0600 Nebula Messenger[1542:262927] [CollectionView] An attempt to prepare a layout while a prepareLayout call was already in progress (i.e. reentrant call) has been ignored. Please file a bug. UICollectionView instance is (<UICollectionView: 0x10703e600; frame = (0 92; 375 491); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x281a741e0>; layer = <CALayer: 0x281431da0>; contentOffset: {0, -8}; contentSize: {375, 0}; adjustedContentInset: {8, 0, 8, 0}> collection view layout: <UICollectionViewFlowLayout: 0x10605fe10>)
2018-10-08 01:10:04.891365-0600 Nebula Messenger[1542:262927] [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:0x283726670 UIView:0x106065480.width == 200   (active)>",
    "<NSLayoutConstraint:0x283726760 UILabel:0x106065190.left == UIView:0x106065480.left + 8   (active)>",
    "<NSLayoutConstraint:0x2837267b0 UILabel:0x106065190.right == UIView:0x106065480.right - 8   (active)>",
    "<NSLayoutConstraint:0x283726850 UILabel:0x106065190.width == 200   (active)>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x283726850 UILabel:0x106065190.width == 200   (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2018-10-08 01:10:04.896386-0600 Nebula Messenger[1542:262927] [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:0x28372bac0 UIView:0x1061705c0.width == 200   (active)>",
    "<NSLayoutConstraint:0x28372bbb0 UILabel:0x1061702d0.left == UIView:0x1061705c0.left + 8   (active)>",
    "<NSLayoutConstraint:0x28372bc00 UILabel:0x1061702d0.right == UIView:0x1061705c0.right - 8   (active)>",
    "<NSLayoutConstraint:0x28372bca0 UILabel:0x1061702d0.width == 200   (active)>"
)

这就是我要滚动的内容:

func scrollToBottom() {
    DispatchQueue.main.async {
        self.messagesCollection.layoutIfNeeded()
        self.messagesCollection?.scrollToItem(at: IndexPath(item: 
            self.msgList.count-1, section: 0), at: 
            UICollectionView.ScrollPosition.bottom, animated: true)
    }
}

当视图加载时,我称之为:

override func viewDidLayoutSubviews(){
        super.viewDidLayoutSubviews()
        self.messagesCollection.layoutIfNeeded()
        self.scrollToBottom()
}

我的第一个问题是它在加载时不会向右滚动,我必须先手动滚动,然后再滚动到底部。

也就是说,除非CollectionView中有很多单元格,否则在这种情况下,它将一直尝试将我向上滚动,直到我与之抗争以使它以其他方式滚动为止。

第二个问题(这似乎是主要问题)是该错误消息。我得到了很多,基本上它只是告诉我必须打破约束才能滚动,但这对我来说没有任何意义。滚动如何打破约束?

放置在scrollToBottomviewDidLoad中的viewDidAppear函数不起作用。

我不反对使用不同的方法来滚动CollectionView,因为无论如何,当前的方法已经让人感到不舒服。

0 个答案:

没有答案