繁体   English   中英

Swift:可以在UICollectionview中添加约束吗?

[英]Swift: is it okay to add constraints to UICollectionview?

我有这堂课

class HomePage: UIViewController,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate

在这个类中,我有很多UIViews.tabView是其中tabView 。现在我想将我的collectionView添加到tabView的底部,我该怎么做? 这是我的`collectionView

    var flowLayout = UICollectionViewFlowLayout()

    let collectionView = UICollectionView(frame: CGRect(x:0,y:500,width:self.view.frame.size.width,height:100   ), collectionViewLayout: flowLayout)
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.backgroundColor = .red`

tabView约束

    tabView.topAnchor.constraint(equalTo: profileInfWrapper.bottomAnchor).isActive = true
    tabView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    tabView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.size.width/4).isActive = true
    tabView.heightAnchor.constraint(equalToConstant: 80).isActive = true

您可以在tabView和设置约束内添加collectionView:

collectionView.translatesAutoresizingMaskIntoConstraints = false
tabView.addSubview(collectionView)

collectionView.topAnchor.constraint(equalTo: tabView.topAnchor, constant: 20).isActive = true

....并添加其他约束

或者,您可以将其添加到选项卡视图下,如下所示:

self.view.addSubview(collectionView)

collectionView.topAnchor.constraint(equalTo: tabView.bottomAnchor, constant: 20).isActive = true

__

顺便说一句,您可以这样设置tabView的宽度:

tabView.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.25).isActive = true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM