集合视图自动布局

时间:2014-10-02 10:35:04

标签: ios xcode swift autolayout xcode6

我正在制作一个在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 个答案:

答案 0 :(得分:1)

在您的示例项目中,您没有设置任何自动布局约束。例如,集合视图应该有4个约束:每个方向一个: constraints

如果将所有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

相关问题