2017-07-18 42 views
1

我想要一個的CollectionView在前沿和後位置添加到我的CustomCollectionViewController的細胞,具有恆定的空間,我心中已經試過錨式和虛擬格式類型,但似乎不主播不按我的要求工作。尾部空間丟失。錨不收集觀察室的佈置工作

這裏是我的代碼:

class CategoryCell: UICollectionViewCell, UICollectionViewDelegateFlowLayout { 
    override init(frame: CGRect) { 
     super.init(frame: frame) 

     setupViews() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    let appView:UICollectionView = { 
     let layout = UICollectionViewFlowLayout() 
     let view = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     view.translatesAutoresizingMaskIntoConstraints = false 
     view.backgroundColor = UIColor.blue 
     return view 
    }() 

    func setupViews() { 
     contentView.addSubview(appView) 
     let layoutGuide = contentView.layoutMarginsGuide 

     appView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor, constant: 8).isActive = true 
     appView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor, constant: 8).isActive = true 
     appView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true 
     appView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true 

//  addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[v0]-8-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": appView])) 
//  addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": appView])) 
    } 

} 

layout with Anchor

但如果你使用舊的虛擬格式的風格,它工作得很好,這讓我感到困惑

layout with virtual format string

回答

1

恆定值trailingAnchor.constraint需要-8而不是8.

appView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor, constant: -8).isActive = true 
+0

是的,簡單的和愚蠢的,你幫了我很多,THX :) – vg0x00

+0

歡迎您:) – Nemanja