Bagaimana cara mengubah arah gulir di UICollectionView?

Saya memiliki UICollectionView di aplikasi iOS berbasis storyboard saya. Jika perangkat dalam orientasi potret, saya ingin perangkat menggulir secara vertikal, dan jika dalam orientasi Landscaper, saya ingin perangkat menggulir secara horizontal.

Di UICollectionView saya dapat melihat anggota scrollEnabled, tetapi saya tidak melihat cara untuk mengatur arah gulir. Apakah saya melewatkan sesuatu?


person Kenny    schedule 27.08.2013    source sumber


Jawaban (6)


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

Perhatikan juga bahwa tampaknya boleh saja memanggil ini di prepareForLayout dalam tata letak alur Anda...

@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);
    }
person user3040186    schedule 06.12.2013
comment
Saya melihat masalah dengan ini, ketika prepLayout kadang-kadang tidak dipanggil cukup awal atau terlambat dipanggil. Hasilnya adalah salah satu dari 3 hal: 1) horizontalSizeClass belum berubah dan arah gulir tidak disetel dengan benar 2) scrollingDirection disetel dengan benar tetapi sudah terlambat dan tidak berpengaruh dan 3) semuanya berbaris dengan benar dan arah gulir sebenarnya berlaku sesuai keinginan. Tampaknya Apple bermaksud agar arah gulir dikelola di pengontrol tampilan, bukan di tata letak. - person BTRUE; 17.04.2017

Setel scrollDirection dari collectionViewLayout tampilan koleksi.

Dokumen adalah di sini.

person Mundi    schedule 27.08.2013
comment
Ini hanya berlaku jika Anda menggunakan UICollectionViewFlowLayout. - person Edward Dale; 03.11.2013
comment
Terima kasih @Mundi, bagaimana Anda mengetahui hal ini? Bagaimanapun, beberapa kode ini untuk catatan saya sendiri: if let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.scrollDirection = .Horizontal } - person Dan Rosenstark; 04.08.2016

Anda harus mencoba ini:

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

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

}
person Allan Scofield    schedule 14.07.2015

Cepat 4 dan 4.2

if let layout = collectionViewObj.collectionViewLayout as? UICollectionViewFlowLayout {
        layout.scrollDirection = .vertical  // .horizontal
    }
person Gurjinder Singh    schedule 11.12.2017

Kudos to Mundi dan Dan Rosenstark atas jawabannya, inilah versi Swift 4.2.

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flowLayout.scrollDirection = .horizontal
}
person Travis M.    schedule 14.09.2017

Lakukan itu di Papan Cerita Anda. Dari pemeriksa identitas dan pilih Arah Gulir dan pilih arah Anda.

masukkan deskripsi gambar di sini

person Wide Angle Technology    schedule 15.09.2017