簡體   English   中英

集合視圖自動布局

[英]Collection view auto layout

我正在制作一個項目,以在iPad上顯示多個圖像,我的問題是我無法使UICollectionView適應屏幕的大小,而且當它旋轉為橫向時,自動布局不起作用,我真的失去了這一點,非常歡迎您的幫助。

這是我在Git上制作的一個簡單示例: Git項目鏈接

這是一些代碼:

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

提前致謝。

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

如果將所有4個約束都設置為0,則其大小將始終與容器相同。

有關更多信息,您應該閱讀Apple開發人員指南: 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM