[英]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.