【问题标题】:Collection view auto layout集合视图自动布局
【发布时间】:2014-10-02 10:35:04
【问题描述】:

我正在制作一个在 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
    }
}

提前致谢。

【问题讨论】:

    标签: ios xcode swift autolayout xcode6


    【解决方案1】:

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

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

    【讨论】:

    • 血腥地狱我无法设置约束...我使用的是 xcode6 beta 6...现在我更新到 xcode 6.1 一切正常,感谢您的帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 2017-05-22
    相关资源
    最近更新 更多