UICollectionView header width

三世轮回 提交于 2019-12-10 21:15:37

问题


In my UICollectionViewFlowLayout subclass I have this:

self.headerReferenceSize = CGSizeMake(280, 44);

However the header is displayed with a width of 320, which is the width of the collection view. This is correct according to the docs:

During layout, only the size that corresponds to the appropriate scrolling direction is used. For example, for the vertical scrolling direction, the layout object uses the height value returned by your method. (In that instance, the width of the header would be set to the width of the collection view.)

However, I need the width to be 280, and not stretch to the entire width of the collection view. How can I override this?


回答1:


You don't need to add a subview to the header.

You want a header width of 280 in a Collection View of 320, so apply a Content Inset of 20 on the left and right, like so:

Swift

YourCollectionView.ContentInset = UIEdgeInsets(top: 0, left: 20, bottom: 20, right: 0)

Xamarin

YourCollectionView.ContentInset = new UIEdgeInsets(0, 20, 0, 20);



回答2:


You can add another custom view over header and resize it as you wish. Ofcourse all you're components that were on header will go over custom view you created...



来源:https://stackoverflow.com/questions/23276993/uicollectionview-header-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!