2013-08-27 32 views
31

Tôi có UICollectionView trong bảng phân cảnh dựa trên ứng dụng iOS của tôi. Khi thiết bị đang ở hướng dọc, tôi muốn nó cuộn theo chiều dọc, và khi nó ở trong Máy quét, tôi muốn nó cuộn theo chiều ngang.Làm cách nào để thay đổi hướng cuộn trong UICollectionView?

Trong chế độ xem UICollection, tôi có thể thấy thành viên cuộnEnabled, nhưng tôi không thấy cách nào để đặt hướng cuộn. Tôi đã bỏ lỡ một cái gì đó?

Trả lời

51
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; 
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; 

Lưu ý quá mà nó có vẻ là tốt để gọi đây là trong prepareForLayout trong cách bố trí dòng chảy của bạn ...

@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); 
    } 
+0

Để khai báo mã này trong viewdidload(); – user3040186

+1

http://adoptioncurve.net/archives/2012/09/a-simple-uicollectionview-tutorial/ – user3040186

+0

Tôi đang gặp vấn đề với điều này, trong đó PrepareLayout không liên tục được gọi là đủ sớm hoặc được gọi là quá muộn. Kết quả là một trong 3 điều: 1) horizontalSizeClass chưa thay đổi và hướng cuộn được đặt không chính xác 2) scrollingDirection được đặt chính xác nhưng đã quá muộn và không có hiệu lực và 3) mọi thứ sắp xếp đúng và hướng cuộn thực sự có hiệu lực như mong muốn. Có vẻ như Apple có ý định di chuyển hướng được quản lý trong bộ điều khiển xem thay vì bố cục. – BTRUE

9

Đặt scrollDirection của chế độ xem bộ sưu tập là collectionViewLayout.

Tài liệu là here.

+2

này chỉ áp dụng nếu bạn đang sử dụng một 'UICollectionViewFlowLayout'. –

+2

Cảm ơn @Mundi, bạn biết công cụ này như thế nào? Dù sao, một số mã này cho các ghi chú của riêng tôi: 'if let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout {flowLayout.scrollDirection = .Horizontal} ' –

9

Bạn nên cố gắng này:

- (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; 
    } 
} 

Trong 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 
    } 

} 
1

Kudos để Mundi và Dan Rosenstark cho câu trả lời của họ, đây là phiên bản 3 nhanh chóng.

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { 
    flowLayout.scrollDirection = .horizontal 
} 
0

Làm điều đó trong bảng phân cảnh của bạn. Từ thanh tra danh tính và chọn hướng và đưa ra hướng đi của bạn. enter image description here

0

Swift 4

if let layout = collectionViewObj.collectionViewLayout as? UICollectionViewFlowLayout { 
     layout.scrollDirection = .vertical // .horizontal 
    } 
Các vấn đề liên quan