Ios 为什么invalidateLayout没有在UICollectionView中触发sizeForItemAtIndexPath?(附代码)
问题是collectionView中的列数在旋转时没有保持在7(所需的数量)。修复此问题需要进行哪些代码更改 自定义UICollectionViewFlowLayout中的invalidateLayout似乎没有触发collectionView中的sizeForItemAtIndexPath方法?有什么想法吗?我真的只希望在旋转时通过sizeForItemAtIndexPath来调整列的大小 注意:这里我没有使用故事板,而是有一个自定义视图,我在其中插入collectionView并引用我自己的collectionView类 下面的代码工作正常,但是在旋转时,它不会像应该的那样将列数保持在7。最初运行代码时,控制台输出显示:Ios 为什么invalidateLayout没有在UICollectionView中触发sizeForItemAtIndexPath?(附代码),ios,swift,rotation,uicollectionview,uicollectionviewlayout,Ios,Swift,Rotation,Uicollectionview,Uicollectionviewlayout,问题是collectionView中的列数在旋转时没有保持在7(所需的数量)。修复此问题需要进行哪些代码更改 自定义UICollectionViewFlowLayout中的invalidateLayout似乎没有触发collectionView中的sizeForItemAtIndexPath方法?有什么想法吗?我真的只希望在旋转时通过sizeForItemAtIndexPath来调整列的大小 注意:这里我没有使用故事板,而是有一个自定义视图,我在其中插入collectionView并引用我自己的
GCCalendar - init coder
GCCalendar - commonInit
GCCalendarLayout:invalidateLayout
GCCalendarLayout:invalidateLayout
ViewController:viewWillLayoutSubviews
GCCalendarLayout:invalidateLayout
GCCalendarLayout:prepareLayout
sizeForItemAtIndexPath
.
.
sizeForItemAtIndexPath
minimumInteritemSpacingForSectionAtIndex
minimumLineSpacingForSectionAtIndex
GCCalendarLayout:collectionViewContentSize
GCCalendarLayout:layoutAttributesForElementsInRect
GCCalendarLayout:collectionViewContentSize
ViewController:viewWillLayoutSubviews
GCCalendarCell:drawRect
.
.
GCCalendarCell:drawRect
但是,旋转屏幕后,我看到以下内容:
ViewController:viewWillLayoutSubviews
GCCalendarLayout:shouldInvalidateLayoutForBoundsChange
GCCalendarLayout:invalidateLayout
GCCalendarLayout:prepareLayout
GCCalendarLayout:collectionViewContentSize
GCCalendarLayout:layoutAttributesForElementsInRect
GCCalendarLayout:collectionViewContentSize
所以问题是“SizeFormiteIndeXPath”从未被调用过???
旋转时输出代码
注意:“SizeFormiteIndeXPath”不会被触发,即使“invalidateLayout”是
ViewController:ViewWillLayoutSubView
GCCalendarLayout:应为边界设置验证布局
GCCalendarLayout:invalidateLayout
**包含集合视图的自定义视图**
import UIKit
@IBDesignable class GCCalendarView: UIView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
// Private
private func commonInit() {
if self.subviews.count == 0 {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "GCCalendarView", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
addSubview(view)
}
}
}
自定义收藏视图
import UIKit
class GCCalendar : UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate {
// Init ---------------
func commonInit(coder aDecoder: NSCoder) {
print("GCCalendar - commonInit")
self.registerClass(GCCalendarCell.self, forCellWithReuseIdentifier: "GCCalendarCell")
self.dataSource = self
self.delegate = self
let layout : GCCalendarLayout = GCCalendarLayout(coder: aDecoder)!
self.setCollectionViewLayout(layout, animated: false)
self.backgroundColor = UIColor.whiteColor()
}
required init?(coder aDecoder: NSCoder) {
print("GCCalendar - init coder")
super.init(coder: aDecoder)
commonInit(coder: aDecoder)
}
// UICollectionViewDelegateFlowLayout ------------
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
print("sizeForItemAtIndexPath")
let w : CGFloat = floor(self.frame.size.width/7)
return CGSize(width: w, height: w)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) ->
CGFloat {
print("minimumInteritemSpacingForSectionAtIndex")
return 0.0
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
print("minimumLineSpacingForSectionAtIndex")
return 0.0
}
// UICollectionViewDataSource -------------------
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 21
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("GCCalendarCell", forIndexPath: indexPath) as? GCCalendarCell
return cell!
}
}
**自定义集合视图单元格**
import UIKit
class GCCalendarCell: UICollectionViewCell {
@IBOutlet weak var title : UITextField!
@IBOutlet weak var date: UILabel!
required init?(coder aDecoder: NSCoder) {
print("GCCalendarCell - init:coder")
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
override func drawRect(rect: CGRect) {
print("GCCalendarCell:drawRect")
self.layer.borderWidth = 1
self.layer.borderColor = UIColor.grayColor().CGColor
}
// Private
private func commonInit() {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "GCCalendarCell", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
addSubview(view)
}
}
自定义布局
import UIKit
class GCCalendarLayout : UICollectionViewFlowLayout {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
print("GCCalendarLayout:shouldInvalidateLayoutForBoundsChange")
return true
}
override func invalidateLayout() {
print("GCCalendarLayout:invalidateLayout")
super.invalidateLayout()
}
override func prepareForCollectionViewUpdates(updateItems: [UICollectionViewUpdateItem]) {
print("GCCalendarLayout:prepareForCollectionViewUpdates")
super.prepareForCollectionViewUpdates(updateItems)
}
override func finalizeCollectionViewUpdates() {
print("GCCalendarLayout:finalizeCollectionViewUpdates")
super.finalizeCollectionViewUpdates()
}
}
如果您使用的是故事板,请在下面所附的故事板I中设置CollectionviewFlowLayout类
编辑:在viewController中尝试以下操作:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
(self.collectionview.collectionViewLayout).setItemSize(CGSizeMake(floor(size.width / 7), floor(size.width / 7)))
self.collectionview.collectionViewLayout.invalidateLayout()
}
@Kujey的解决方案可以应用于
UIView
,而无需访问UIViewController
:
func traitCollectionDidChange(previousTraitCollection : UITraitCollection) {
super.traitCollectionDidChange(previousTraitCollection)
self.collectionView.collectionViewLayout.invalidateLayout()
}
现有答案对我没有任何帮助(iPhone X模拟器iOS 11.2)。
我发现,更改的有助于解决此问题:
<...>
//***new line
flowLayout.estimatedItemSize = [self getIconSize];//get a new size for items
[flowLayout invalidateLayout];
<...>
//***新线
flowLayout.estimatedItemSize=[self-getIconSize]//获取项目的新尺寸
[flowLayout invalidateLayout];
您是否在使用故事板否。不使用故事板我可以帮你,但我不知道斯威夫特。你能读懂obj-c吗?是的,这应该是好的,我必须有这个权利,因为它在运行时确实设置了正确的尺寸,它只是在旋转时改变问题可能是的。但是,您是否可以在自定义flowlayout的itemSize函数中添加一个日志,看看它是否在旋转时被调用。sizeForItemAtIndexPath(如果这是您的意思)在旋转时没有被调用……这就是issue@EricAya我认为编程语言对于这个问题并不重要。