我在这个功能中有一个按钮
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellid", for: indexPath)
let editButton = UIButton()
editButton.translatesAutoresizingMaskIntoConstraints = false
editButton.addTarget(self, action: #selector(click), for: .touchUpInside)
editButton.backgroundColor = UIColor(red: 0/255, green: 49/255, blue: 142/255, alpha: 1.0)
editButton.setTitle("Register", for: [])
cell.addSubview(editButton)
return cell
我如何添加这样的约束?
editButton.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 16).isActive = true
答案 0 :(得分:0)
试试这个:
editButton.leftAnchor.constraint(equalTo: cell.contentView.leftAnchor, constant: 16).isActive = true
此外,最好制作自定义UICollectionViewCell
并在那里或在故事板中配置您的按钮,而不是在collectionView(_: UICollectionView, cellForItemAt: IndexPath)