2014-10-02 60 views
0

我正在做一個項目,以在iPad上顯示多個圖像,我的問題是我無法使UICollectionView適應屏幕的大小,並且當它旋轉以橫向自動佈局不工作,我真的失去了這一點,任何幫助是非常值得歡迎的。收集視圖自動佈局

下面是一個簡單的例子,我在做的Git:Git project link

下面是一些代碼:

class ViewController: UIViewController { 

    @IBOutlet weak var mainMenuCollectionView: UICollectionView! 
    var images = ["220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg","220.jpg"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
     return 1 
    } 

    func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int 
    { 
     return images.count 
    } 

    func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! 
    { 
     var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as HomeCollectionViewCell 
     cell.imageView.image = UIImage(named:images[indexPath.row]) 
     cell.textLabel.text = "Woof Woof" 
     return cell 
    } 
} 

在此先感謝。

回答

1

在您的示例項目中,您沒有設置任何自動佈局約束。例如,收集視圖應該有4個約束:每個方向一個: constraints

如果將所有4個約束設置爲0,它將始終具有與容器相同的大小。

欲瞭解更多信息,請閱讀蘋果的開發者指南:https://developer.apple.com/library/IOs/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/Introduction.html

或者與一些教程開始: http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1

+0

該死我不能設置的約束......我用xcode6測試6。 ..現在我更新到Xcode 6.1,一切工作正常,感謝您的幫助:) – Bruno 2014-10-02 11:00:07