Ios 斯威夫特:可以向UICollectionview添加约束吗?
我有这门课Ios 斯威夫特:可以向UICollectionview添加约束吗?,ios,swift,Ios,Swift,我有这门课 class HomePage: UIViewController,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate 在这个类中,我有很多UIView。其中一个是tabView。现在我想将我的collectionView添加到tabView的底部,我该怎么做?这是我的“收藏视图” var flowLayout = UICollectionViewFl
class HomePage: UIViewController,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate
在这个类中,我有很多UIView。其中一个是tabView
。现在我想将我的collectionView
添加到tabView
的底部,我该怎么做?这是我的“收藏视图”
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.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
可以在选项卡视图和设置约束中添加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.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.25).isActive = true
可以在选项卡视图和设置约束中添加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.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.25).isActive = true
运行后您是否遇到任何问题。@KrishnarjunBanoth在此代码之后没有,但我没有尝试添加锚。我想知道是否可以将锚添加到uicollectionview。是的,向其添加约束完全可以collectionView@user1000好的,谢谢,请提出你问题的解决方案,不要只问关于你想要开发的功能的问题。在运行它之后,你有没有遇到任何问题。@KrishnarjunBanoth在此代码之后没有,但我还没有尝试添加锚。我想知道是否可以将它们添加到uicollectionview。是的,向其添加约束完全可以collectionView@user1000好的,谢谢,请为您的问题寻求解决方案,不要只问关于您想要开发的功能的问题。