代码之家  ›  专栏  ›  技术社区  ›  Ofri

约束从视图中删除SegmentController

  •  -1
  • Ofri  · 技术社区  · 8 年前

    我有一个 uiCollectionView 和一个 SegmentController 。

    所需的最终结果:

    SegmentController fully in view, uiCollectionView belower it

    添加约束之前:

    添加约束后(请注意, SegmentController 几乎完全隐藏):。

    添加的约束:

    productsCollection.removeConstraints(productsCollection.Constraints) segmentcontroller.translatesAutoResizingMaskinToConstraints=false

    nslayoutConstraint.activate([
    segmentcontroller.centerXanchor.constraint(等于:view.centerXanchor)
    SegmentController.TopAnchor.Constraint(等于:self.view.safearelayoutguide.TopAnchor,常量:100),
    //productsCollection.TopAnchor.Constraint(EqualTo:SegmentController.BottomAnchor,常量:10),
    productsCollection.LeftAnchor.Constraint(等于:View.LeftAnchor),
    productsCollection.RightAnchor.Constraint(等于:view.RightAnchor),
    productsCollection.BottomAnchor.Constraint(等于:view.BottomAnchor)
    )
    < /代码> 
    
    

    我猜问题是我没有添加SegmentController约束,但添加时得到了相同的结果:

    SegmentController.TopAnchor.Constraint(等于:self.view.safearelayoutGuide.TopAnchor,常量:100),
    segmentcontroller.centerXanchor.constraint(等于:view.centerXanchor)
    < /代码> 
    
    <>编辑:

    此视图继承自另一个视图,该视图具有:

    private func createproductsCollection())
    {
    let-layout:uiCollectionViewFlowLayout=uiCollectionViewFlowLayout()。
    layout.itemsize=cgsize(宽200高250)
    
    self.productsCollection=uiCollectionView(框架:self.view.frame,集合视图布局:布局)
    
    productsCollection.datasource=自己
    productsCollection.delegate=自己
    productsCollection.register(productsCollectionViewCell.self,forcellWithReuseIdentifier:“product_collection_cell”)。
    productsCollection.backgroundcolor=uicolor.clear
    self.view.addSubView(产品集合)
    
    productsCollection.translatesAutoResizingMaskinToConstraints=false
    
    nslayoutConstraint.activate([
    nslayoutConstraint(项:ProductsCollection,属性:.Top,RelatedBy:.Equal,ToItem:Self.View,属性:.Top,乘数:1,常量:20),
    nslayoutConstraint(item:productsCollection,attribute:.bottom,relatedby:.equal,toitem:self.view,attribute:.bottom,multipler:1,constant:-50),//为搜索字段保留空间
    nslayoutConstraint(项:ProductsCollection,属性:.Leading,RelatedBy:.Equal,ToItem:Self.View,属性:.Leading,乘数:1,常量:0),
    nslayoutConstraint(项:ProductsCollection,属性:.Trailing,RelatedBy:.Equal,ToItem:Self.View,属性:.Trailing,乘数:1,常量:0)
    )
    }
    < /代码> 
    
    <编辑> 2:

    我现在使用这些约束:

    SegmentController.TranslatesAutoResizingMaskinToConstraints=false
    segmentcontroller.topanchor.constraint(等于:self.view.safearelayoutguide.topanchor).isactive=true
    SegmentController.HeightAnchor.Constraint(EqualToConstant:40).IsActive=true
    segmentcontroller.widthanchor.constraint(equalToconstant:120).isactive=true
    segmentcontroller.centerXanchor.constraint(等于:self.view.centerXanchor).isactive=true
    
    self.view.addSubView(产品集合)
    productsCollection.translatesAutoResizingMaskinToConstraints=false
    productsCollection.TopAnchor.Constraint(等于:self.SegmentController.BottomAnchor).IsActive=true
    productsCollection.trailinganchor.constraint(等于:self.view.trailinganchor).isactive=true
    productsCollection.BottomAnchor.Constraint(等于:self.view.BottomAnchor).IsActive=true
    productsCollection.LeadingAnchor.Constraint(等于:self.view.LeadingAnchor).IsActive=true
    < /代码> 
    
    

    现在我的观点是:

    .ui集合视图在它下面

    添加约束之前:

    enter image description here

    添加约束后(请注意分段控制器几乎完全隐藏):

    enter image description here

    添加的约束:

    productsCollection.removeConstraints(productsCollection.Constraints) SegmentController.TranslatesAutoResizingMaskinToConstraints=false

    NSLayoutConstraint.activate([
        SegmentController.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        SegmentController.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 100),
        //ProductsCollection.topAnchor.constraint(equalTo: SegmentController.bottomAnchor, constant: 10),
        ProductsCollection.leftAnchor.constraint(equalTo: view.leftAnchor),
        ProductsCollection.rightAnchor.constraint(equalTo: view.rightAnchor),
        ProductsCollection.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    

    我想问题是我没有添加SegmentController约束,但添加时得到了相同的结果:

    SegmentController.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 100),
    SegmentController.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    

    编辑:

    此视图继承自另一个视图,该视图具有:

        private func createProductsCollection()
            {
                let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
                layout.itemSize = CGSize(width: 200, height: 250)
    
                self.ProductsCollection = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    
                ProductsCollection.dataSource = self
                ProductsCollection.delegate = self
                ProductsCollection.register(ProductsCollectionViewCell.self, forCellWithReuseIdentifier: "product_collection_cell")
                ProductsCollection.backgroundColor = UIColor.clear
                self.view.addSubview(ProductsCollection)
    
    ProductsCollection.translatesAutoresizingMaskIntoConstraints = false
    
    NSLayoutConstraint.activate([
                    NSLayoutConstraint(item: ProductsCollection, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 20),
                    NSLayoutConstraint(item: ProductsCollection, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -50), //leaving space for search field
                    NSLayoutConstraint(item: ProductsCollection, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0),
                    NSLayoutConstraint(item: ProductsCollection, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0)
                    ])
            }
    

    编辑第2页:

    我现在使用这些约束:

    SegmentController.translatesAutoresizingMaskIntoConstraints = false
            SegmentController.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
            SegmentController.heightAnchor.constraint(equalToConstant: 40).isActive = true
            SegmentController.widthAnchor.constraint(equalToConstant: 120).isActive = true
            SegmentController.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    
            self.view.addSubview(ProductsCollection)
            ProductsCollection.translatesAutoresizingMaskIntoConstraints = false
            ProductsCollection.topAnchor.constraint(equalTo: self.SegmentController.bottomAnchor).isActive = true
            ProductsCollection.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
            ProductsCollection.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
            ProductsCollection.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
    

    现在我的观点是:

    2 回复  |  直到 8 年前
        1
  •  1
  •   Galo Torres Sevilla    8 年前

    您的约束不正确。它不应该是 self.view.safeAreaLayoutGuide.bottomAnchor 应该是 self.view.safeAreaLayoutGuide.topAnchor . 检查安全区域指南给出的确切内容。来自苹果:

    表示视图部分的布局指南 不受酒吧和其他内容的影响。

    从这一点很容易看出,它是“边缘”内的区域。

    编辑

            self.view.addSubview(segmentController)
            segmentController.translatesAutoresizingMaskIntoConstraints = false
            segmentController.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
            segmentController.heightAnchor.constraint(equalToConstant: 40).isActive = true
            segmentController.widthAnchor.constraint(equalToConstant: 120).isActive = true
            segmentController.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    
            self.view.addSubview(collectionView)
            collectionView.translatesAutoresizingMaskIntoConstraints = false
            collectionView.topAnchor.constraint(equalTo: self.segmentController.bottomAnchor).isActive = true
            collectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
            collectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
            collectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true 
    

    您还需要删除此框架

    self.ProductsCollection = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    

    把它改成 frame: .zero 剩下的就由禁忌症来解决了

        2
  •  2
  •   Shehata Gamal    8 年前

    你可以试试

    nslayoutConstraint.activate([
    segmentcontroller.centerXanchor.constraint(等于:view.centerXanchor)
    SegmentController.TopAnchor.Constraint(等于:self.view.safearelayoutguide.TopAnchor,常量:100),
    productsCollection.TopAnchor.Constraint(等于:SegmentController.BottomAnchor,常量:10),
    productsCollection.LeftAnchor.Constraint(等于:View.LeftAnchor),
    productsCollection.RightAnchor.Constraint(等于:view.RightAnchor),
    productsCollection.BottomAnchor.Constraint(等于:view.BottomAnchor)
    )
    < /代码> 
    
    

    注:节段顶部应钩住视图顶部,而不是底部,如您在此处所做的。

    SegmentController.TopAnchor.Constraint(等于:self.view.safearelayoutGuide.BottomAnchor,
    < /代码> 
    
    

    //

    let segmentcontroller=uiSegmentedControl(items:[“safe”,“ask”])
    
    让productsCollection=uiView()。
    
    ProductsCollection.BackgroundColor=.Red
    
    View.AddSubView(分段控制器)
    
    View.AddSubView(产品集合)
    
    SegmentController.TranslatesAutoResizingMaskinToConstraints=false
    
    productsCollection.translatesAutoResizingMaskinToConstraints=false
    
    nslayoutConstraint.activate([
    segmentcontroller.centerXanchor.constraint(等于:view.centerXanchor)
    SegmentController.TopAnchor.Constraint(等于:self.view.safearelayoutguide.TopAnchor,常量:100),
    productsCollection.TopAnchor.Constraint(等于:SegmentController.BottomAnchor,常量:10),
    productsCollection.LeftAnchor.Constraint(等于:View.LeftAnchor),
    productsCollection.RightAnchor.Constraint(等于:view.RightAnchor),
    productsCollection.BottomAnchor.Constraint(等于:view.BottomAnchor)
    )
    < /代码> 
    
    

    注:节段顶部应钩住视图顶部,而不是底部,如您在此处所做的。

    SegmentController.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor,
    

    / /

    let SegmentController = UISegmentedControl(items: ["Safe","Ask"])
    
    let ProductsCollection = UIView()
    
    ProductsCollection.backgroundColor = .red
    
    view.addSubview(SegmentController)
    
    view.addSubview(ProductsCollection)
    
    SegmentController.translatesAutoresizingMaskIntoConstraints = false
    
    ProductsCollection.translatesAutoresizingMaskIntoConstraints = false
    
    NSLayoutConstraint.activate([
        SegmentController.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        SegmentController.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 100),
        ProductsCollection.topAnchor.constraint(equalTo: SegmentController.bottomAnchor, constant: 10),
        ProductsCollection.leftAnchor.constraint(equalTo: view.leftAnchor),
        ProductsCollection.rightAnchor.constraint(equalTo: view.rightAnchor),
        ProductsCollection.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    ])
    

    enter image description here

    推荐文章