ฉันจะเปลี่ยนทิศทางการเลื่อนใน UICollectionView ได้อย่างไร

ฉันมี UICollectionView ในแอป iOS ที่ใช้สตอรี่บอร์ด เมื่ออุปกรณ์อยู่ในแนวตั้ง ฉันต้องการให้เลื่อนในแนวตั้ง และเมื่ออยู่ในแนวนอน ฉันต้องการให้เลื่อนในแนวนอน

ใน UICollectionView ฉันสามารถเห็นสมาชิก scrollEnabled แต่ฉันไม่เห็นวิธีกำหนดทิศทางการเลื่อน ฉันพลาดอะไรบางอย่างไปหรือเปล่า?


person Kenny    schedule 27.08.2013    source แหล่งที่มา


คำตอบ (6)


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

โปรดทราบด้วยว่าดูเหมือนว่าจะเป็นการดีที่จะเรียกสิ่งนี้ใน 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);
    }
person user3040186    schedule 06.12.2013
comment
ฉันเห็นปัญหาเกี่ยวกับเรื่องนี้ โดยที่การpreparLayoutไม่ได้รับการเรียกเป็นระยะๆ เร็วพอหรือสายเกินไป ผลลัพธ์คือหนึ่งใน 3 สิ่ง: 1) แนวนอนSizeClass ยังไม่เปลี่ยนแปลงและตั้งค่าทิศทางการเลื่อนไม่ถูกต้อง 2) ตั้งค่า scrollingDirection อย่างถูกต้อง แต่สายเกินไปและไม่มีผล และ 3) ทุกอย่างเข้าแถวอย่างถูกต้องและทิศทางการเลื่อน ได้ผลจริงตามต้องการ ดูเหมือนว่า Apple อาจตั้งใจให้จัดการทิศทางการเลื่อนในตัวควบคุมมุมมองแทนที่จะเป็นเค้าโครง - person BTRUE; 17.04.2017

ตั้งค่า scrollDirection ของมุมมองคอลเลกชันเป็น collectionViewLayout

เอกสารคือ ที่นี่

person Mundi    schedule 27.08.2013
comment
สิ่งนี้ใช้ได้เฉพาะเมื่อคุณใช้ UICollectionViewFlowLayout - person Edward Dale; 03.11.2013
comment
ขอบคุณ @Mundi คุณรู้จักสิ่งนี้ได้อย่างไร อย่างไรก็ตาม รหัสนี้สำหรับบันทึกของฉันเอง: if let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.scrollDirection = .Horizontal } - person Dan Rosenstark; 04.08.2016

คุณควรลองสิ่งนี้:

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

ในสวิฟท์:

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

สวิฟท์ 4 และ 4.2

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

ขอชื่นชม Mundi และ Dan Rosenstark สำหรับคำตอบ นี่คือเวอร์ชัน 4.2 ที่รวดเร็ว

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

ทำสิ่งนั้นในสตอรี่บอร์ดของคุณ จากตัวตรวจสอบตัวตนและเลือกทิศทางการเลื่อนและเลือกทิศทางของคุณ

ป้อนคำอธิบายรูปภาพที่นี่

person Wide Angle Technology    schedule 15.09.2017