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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:53:13  来源:igfitidea点击:

How can I change the scroll direction in UICollectionView?

iosobjective-cscrolluicollectionvieworientation

提问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 prepareForLayoutin 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

Set the scrollDirectionof the collection view's collectionViewLayout.

设置scrollDirection集合视图的collectionViewLayout.

Docs are here.

文档在这里

回答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
}

回答by Gregory Wilson Pullyattu

Do that in your storyboard. From identity inspector and select direction and give your direction. enter image description here

在你的故事板中做到这一点。来自身份检查员并选择方向并给出您的指示。 在此处输入图片说明