Swift 添加约束混乱视图
我有一个带有Swift 添加约束混乱视图,swift,xcode,uicollectionview,constraints,uisegmentedcontrol,Swift,Xcode,Uicollectionview,Constraints,Uisegmentedcontrol,我有一个带有UICollectionView和UISegmentedControl的视图 我想更改约束,使分段控制器不会与集合视图重叠,如下图所示: 这是我的代码: override func viewDidLoad() { super.viewDidLoad() self.navigationItem.leftBarButtonItem = nil self.tabBarController?.tabBar.isHidden = tr
UICollectionView
和UISegmentedControl
的视图
我想更改约束,使分段控制器不会与集合视图重叠,如下图所示:
这是我的代码:
override func viewDidLoad()
{
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = nil
self.tabBarController?.tabBar.isHidden = true
self.SegmentController.setTitle(SegmentAtext, forSegmentAt: 0)
self.SegmentController.setTitle(SegmentBtext, forSegmentAt: 1)
self.view.bringSubview(toFront: SegmentController)
self.LoadProducts(productsToShow: SegmentAtype)
}
因此,我添加了以下命令:
self.ProductsCollection.topAnchor.constraint(equalTo: SegmentController.bottomAnchor, constant: 10).isActive = true
但结果更糟:
现在,段控制器几乎完全隐藏
我该如何解决这个问题
编辑:
My viewDidLayoutSubviews函数:
override func viewDidLayoutSubviews()
{
ProductsCollection.translatesAutoresizingMaskIntoConstraints = false
let topConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 20)
let bottomConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -50) //leaving space for search field
let leadingConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: ProductsCollection, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0)
self.view.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])
}
注意:
myviewDidLayoutSubviews
在超级视图中实现,超级视图不包含UISegmentedControl
。UISegmentedControl
包含在继承视图中
编辑:更新的视图
如果您希望
UISegmentedControl
位于屏幕中央,并在其下方显示收藏视图,您可以这样做
NSLayoutConstraint.activate([
segmentedControl.topAnchor.constraint(equalTo: view.topAnchor),
segmentedControl.centerXAnchor.constraint(equalTo: view.centerXAnchor),
collectionView.topAnchor.constraint(equalTo: segmentedControl.bottomAnchor),
collectionView.leftAnchor.constraint(equalTo: view.leftAnchor),
collectionView.rightAnchor.constraint(equalTo: view.rightAnchor),
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
因此,我们将分段控件置于视图顶部,并将其居中,
然后,集合视图的顶部是分段控件的底部(如果需要,可以添加一个用于填充的常量),而左侧、右侧和底部则是视图的底部期望的结果是什么?我希望集合视图位于SegmentControl@Ladislav参见我的答案:)什么是分段控制器?你是说
UISegmentedControl
还是?同时粘贴当前布局的代码UICollectionView
,然后我可以帮助你将两者的translatesAutoResistingGMaskintoConstraints
设置为false
?哦,等等。我忘了提到我有一个导航控制器在上面,所以我猜它隐藏在它后面?或者你可以尝试用segmentedControl.topAnchor.constraint(equalTo:view.safeAreaLayoutGuide.topAnchor)修复第一个约束(equalTo:view.safeAreaLayoutGuide.topAnchor)
我添加了我整个视图的图像:)尝试替换我在上面的评论中提到的约束