Ios UICollectionView未锚定到我的UIView顶部
因此,我试图将UICollectionView锚定到视图的顶部。我用锚来帮助我。这是我目前掌握的代码:Ios UICollectionView未锚定到我的UIView顶部,ios,swift,constraints,Ios,Swift,Constraints,因此,我试图将UICollectionView锚定到视图的顶部。我用锚来帮助我。这是我目前掌握的代码: class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { let cellID : String = "menuCell" lazy var collectionView : UICollectionV
class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let cellID : String = "menuCell"
lazy var collectionView : UICollectionView = {
let cv : UICollectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
cv.backgroundColor = red
cv.translatesAutoresizingMaskIntoConstraints = false
//cv.isScrollEnabled = false
cv.delegate = self
cv.dataSource = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupCollectionView()
}
func setupCollectionView() {
addSubview(collectionView)
collectionView.register(MenuBarCell.self, forCellWithReuseIdentifier: cellID)
// setup menuBar constraints
collectionView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
collectionView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
collectionView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
collectionView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)
cell.backgroundColor = .white
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
}
我使用以下两段代码将菜单栏添加到ViewController中:
let menuBar : MenuBar = {
let mb = MenuBar()
mb.translatesAutoresizingMaskIntoConstraints = false
return mb
}()
func setupMenuBar() {
view.addSubview(menuBar)
menuBar.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
menuBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
menuBar.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
menuBar.heightAnchor.constraint(equalToConstant: 200).isActive = true
}
在viewDidLoad
中,我调用setupMenuBar
但是UICollectionView似乎总是从视图顶部偏移一定数量的像素,但我看不出发生这种情况的任何原因。这就是我所看到的:
基本上我想要的是让最左边的单元格位于视图的左上角
有人能帮我弄清楚吗?提前谢谢你 当您将
print(collectionView.contentInset.top)
放入时,它会打印什么?@LinusGeffarth它会打印0.0
您是否通过代码创建此视图?或者在Interface Builder中设置UIView的自定义类?如果通过代码,您能否显示用于创建和添加菜单栏视图的代码?
?是的-这是一个时间问题(显然)。如果将setupMenuBar()
移动到viewdide()
中,它将正确布局。在那之前的任何时候,它都不会。我尝试了几件事情来解决它,但没有运气(我的时间不够)。但这绝对是你需要遵循的路径来找到解决方案。哎哟!!!这是.automaticallyaadjustsscrollviewsinsets
。。。尝试将您的didFinishLaunching
更改为:当您将print(collectionView.contentInset.top)
放入时,它会打印什么?@LinusGeffarth它会打印0.0
您是否通过代码创建此视图?或者在Interface Builder中设置UIView的自定义类?如果通过代码,您能否显示用于创建和添加菜单栏视图的代码?
?是的-这是一个时间问题(显然)。如果将setupMenuBar()
移动到viewdide()
中,它将正确布局。在那之前的任何时候,它都不会。我尝试了几件事情来解决它,但没有运气(我的时间不够)。但这绝对是你需要遵循的路径来找到解决方案。哎哟!!!这是.automaticallyaadjustsscrollviewsinsets
。。。尝试将您的didfishLaunching
更改为: