【发布时间】:2017-08-18 00:58:13
【问题描述】:
我正在尝试添加一个堆栈视图,其中包含顶部的 UILabel 和下方的 UICollectionView。我试图通过将堆栈视图锚定到所有侧面来限制堆栈视图,使其占据整个视图。当我运行该应用程序时,会出现 UILabel 并出现集合视图的一部分。集合视图说它的宽度和高度都为零。任何帮助将不胜感激,谢谢。
var collectionView: UICollectionView!
var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view.translatesAutoresizingMaskIntoConstraints = false
let margins = self.view.layoutMarginsGuide
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
layout.itemSize = CGSize(width: self.collectionView.frame.width / 4, height: self.collectionView.frame.width / 4)
layout.minimumInteritemSpacing = self.collectionView.frame.width / 15
layout.minimumLineSpacing = self.collectionView.frame.width / 5
collectionView.backgroundColor = UIColor.black
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
titleLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.textAlignment = .center
titleLabel.numberOfLines = 1
titleLabel.font = UIFont.systemFont(ofSize: 22)
titleLabel.backgroundColor = UIColor.lightGray
titleLabel.text = "Challenges"
titleLabel.textColor = UIColor.red
let stackView = UIStackView(arrangedSubviews: [titleLabel, collectionView])
stackView.backgroundColor = UIColor.white
stackView.axis = .vertical
stackView.distribution = .fillEqually
stackView.alignment = .fill
stackView.spacing = 5
self.view.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
//stackView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
//stackView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
stackView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
stackView.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 1.0).isActive = true
}
更新:
我获取了我的代码并在一个新的单一视图应用程序中运行它,它按预期工作。因此,我认为值得一提的是,我正在尝试将其合并到精灵工具包游戏中,并通过这样做来展示视图控制器:
let root = self.view?.window?.rootViewController
var viewC = SelectionCollectionViewController()
root?.present(viewC, animated: false, completion: nil)
我需要采取什么特殊步骤,因为这是使用 sprite kit 完成的吗?
【问题讨论】:
-
一些事情,我认为这些都不重要。 (1) 尽可能不要使用框架。换句话说,将集合视图的框架设置为
CGRect.zero。同样,我不相信这有什么不同。 (2) 在viewDidLoad中设置你的约束,但是——特别是如果你使用自动布局——不要在那里引用任何子视图框架。无论如何,您很可能会拥有CGRect.zero。因此,请在viewWillLayoutSubviews中设置您的“布局”内容(我认为使用框架可能是正确的)。 (3) 如果您设置了其他 4 个约束,则无需设置heightAnchor。摆脱它。 -
最后一点(我的房间用完了)。在这三件事中,我打赌第二件事最有可能发挥作用。
-
我执行了代码,它显示了 UILabel - Spacing of 5 pts - CollectionView。预期的输出是什么?
-
预期输出是什么?
-
@dfd 感谢您的帮助!我将代码连同您的其他建议一起放入 ViewWillLayoutSubviews(),但似乎并没有解决问题。当我切换我的 collectionView 的框架而不是让我得到的 2 个红色矩形时它们消失了。
标签: ios swift xcode autolayout uistackview