gpt4 book ai didi

ios - Collection View 自动布局

转载 作者:行者123 更新时间:2023-11-29 02:37:47 26 4
gpt4 key购买 nike

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

提前致谢。

最佳答案

在您的示例项目中,您没有设置任何自动布局约束。例如 Collection View 应该有 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

关于ios - Collection View 自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26159041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com