添加约束混乱视图

时间:2018-08-31 09:28:33

标签: swift xcode uicollectionview constraints uisegmentedcontrol

我有一个summaryLastRowD的视图。

我想更改约束,以便段控制器不会与集合视图重叠,如下图所示:

enter image description here

这是我的代码:

UICollectionView

所以我添加了以下命令:

UISegmentedControl

但是结果更糟:

enter image description here

现在段控制器几乎完全隐藏了!

我该如何解决?

编辑:

我的viewDidLayoutSubviews函数:

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 在超级视图中实现,该视图不包含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]) } viewDidLayoutSubviews包含在继承视图中。

编辑:更新的视图

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您希望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)
        ])

因此,我们将控件分割成视图的顶部,并将其居中, 然后集合视图的顶部是分段控件的底部(您可以根据需要添加用于填充的常量),并在视图的左,右和底部