ios 如何更改 UICollectionView 中的滚动方向?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18464153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change the scroll direction in UICollectionView?
提问by Kenny
I have a UICollectionViewin my storyboard based iOSapp. When the device is in the portrait orientation I'd like it to scroll vertically, and when it's in Landscaper I'd like it to scroll horizontally.
我的基于故事板的iOS应用程序中有一个UICollectionView。当设备处于纵向时,我希望它垂直滚动,而当它在 Landscaper 中时,我希望它水平滚动。
In UICollectionView I can see the scrollEnabledmember, but I can see no way to set the scroll direction. Have I missed something?
在 UICollectionView 中,我可以看到scrollEnabled成员,但我看不到设置滚动方向的方法。我错过了什么吗?
回答by user3040186
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
Note too that it seems to be fine to call this in prepareForLayout
in your flow layout...
还要注意,prepareForLayout
在您的流程布局中调用它似乎没问题...
@interface LayoutHorizontalThings : UICollectionViewFlowLayout
@end
@implementation LayoutHorizontalBooks
-(void)prepareLayout
{
[super prepareLayout];
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.minimumInteritemSpacing = 0;
self.minimumLineSpacing = 0;
self.itemSize = CGSizeMake(110,130);
self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
回答by Mundi
回答by Gurjinder Singh
Swift 4 and 4.2
斯威夫特 4 和 4.2
if let layout = collectionViewObj.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .vertical // .horizontal
}
回答by Allan Scofield
You should try this:
你应该试试这个:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)[self.collectionView collectionViewLayout];
if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)){
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
}
else{
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
}
In Swift:
在斯威夫特:
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
var layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
if ((toInterfaceOrientation == UIInterfaceOrientation.LandscapeLeft) || (toInterfaceOrientation == UIInterfaceOrientation.LandscapeRight)){
layout.scrollDirection = UICollectionViewScrollDirection.Vertical
}
else{
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
}
}
回答by Travis M.
Kudos to Mundi and Dan Rosenstark for their answer, here's the swift 4.2 version.
感谢 Mundi 和 Dan Rosenstark 的回答,这是 swift 4.2 版本。
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.scrollDirection = .horizontal
}