rightAnchor constraint did not apply when added programmatically - swift

后端 未结 2 1771
被撕碎了的回忆
被撕碎了的回忆 2021-01-23 11:40

When I Added rightAnchor constraint, constant= 20 did not apply. In leftAnchor is ok

override init(frame: CGRect) {
    super.init(frame: frame)

    addSubview         


        
2条回答
  •  庸人自扰
    2021-01-23 12:39

    You are adding 20+rightAnchor of your superview. It should be -20

    addSubview(collectionView)
    collectionView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([collectionView.leftAnchor.constraint(equalTo: leftAnchor, constant: 20),
                                 collectionView.topAnchor.constraint(equalTo: self.topAnchor),
                                 collectionView.rightAnchor.constraint(equalTo: rightAnchor, constant: -20),
                                 collectionView.heightAnchor.constraint(equalTo: self.heightAnchor)])
    

提交回复
热议问题