Ken*_*nny 36 scroll objective-c orientation ios uicollectionview
我的故事板iOS应用程序中有一个UICollectionView.当设备处于纵向方向时,我希望它能够垂直滚动,当它在Landscaper中时,我希望它能够水平滚动.
在UICollectionView中,我可以看到scrollEnabled成员,但我看不到设置滚动方向的方法.我错过了什么吗?
小智 62
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
Run Code Online (Sandbox Code Playgroud)
另请注意,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);
}
Run Code Online (Sandbox Code Playgroud)
你应该试试这个:
- (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;
}
}
Run Code Online (Sandbox Code Playgroud)
在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
}
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特4和4.2
if let layout = collectionViewObj.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .vertical // .horizontal
}
Run Code Online (Sandbox Code Playgroud)
感谢 Mundi 和 Dan Rosenstark 的回答,这是 swift 4.2 版本。
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.scrollDirection = .horizontal
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
55427 次 |
最近记录: |