2013-08-27 16 views
31

UICollectionView, storyboard tabanlı iOS uygulamasında var. Cihaz dikey yönde olduğunda, dikey olarak kaydırmak isterim ve Landscaper'dayken yatay kaydırma yapmak isterim.UICollectionView'da kaydırma yönünü nasıl değiştirebilirim?

UICollectionView uygulamasında scrollEnabled üye görüyorum, ancak kaydırma yönünü ayarlamanın bir yolunu göremiyorum. Bir şey mi özledim?

cevap

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

Not ...

@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

Bu kodu viewdidload() içinde bildirmek için; – user3040186

+1

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

+0

Bununla ilgili sorunları görüyorum; burada readyLayout aralıklı olarak yeterince erken çağrılmıyor veya çok geç çağrılıyor. Sonuçlar 3 şeyden biri: 1) horizontalSizeClass henüz değişmedi ve kaydırma yönü yanlış ayarlanmış 2) scrollingDirection doğru ayarlandı ama çok geç ve etkili olmaz ve 3) her şey düzgün bir şekilde ve kaydırma yönünde aslında istenen şekilde etkili olur. Görünüşe göre belki de Apple, düzen yerine görünüm denetleyicisinde yönetilecek yönü kaydırmayı amaçlamaktadır. – BTRUE

9

Koleksiyon görünümünün collectionViewLayout ürününün scrollDirection değerini ayarlayın.

Belgeler, here'dur. sizin akış düzeninde prepareForLayout bu çağırmak için iyi olacak gibi görünüyor bu da

+2

bir 'UICollectionViewFlowLayout' kullanıyorsanız bu geçerlidir. –

+2

Teşekkürler @Mundi, bunları nasıl biliyorsunuz? Neyse, bazı kodları kendi notlarım için kodlayın: eğer flowLayout = collectionViewLayout olarak izin verirse? UICollectionViewFlowLayout {flowLayout.scrollDirection = .Horizontal} ' –

9

Bunu denemek gerekir:

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

yılında Hızlı:

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

Mundi ve Dan Rosenstark'ın cevabı için kuduzlar, hızlı 3 versiyon.

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

Bunu, storyboard'unuzda yapın. Kimlik denetçisinden ve yönünü seç ve yönünü ver. enter image description here

0

Swift 4

if let layout = collectionViewObj.collectionViewLayout as? UICollectionViewFlowLayout { 
     layout.scrollDirection = .vertical // .horizontal 
    } 
İlgili konular