それは次のようになりますので、私はプログラム的ViewCell
を作成していると私はUIImageView
にUIButton
を中央にしたい:最初のinitメソッドでは、私はUIImageView
とUIButton
を作成することを実現するためにUIImageViewでUIButtonをプログラムで中央に配置する方法
-----------------------------
| |
| |
| Button |
| |
| |
-----------------------------
:
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
imageView.isUserInteractionEnabled = true
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "button_image"), for: .normal)
button.contentMode = .center
button.frame = CGRect(x: 0, y: 0, width: 42, height: 42)
そしていくつかの制約を追加します。ボタンとイメージビューの場合、これらの制約を追加しました:
button.addConstraint(NSLayoutConstraint(item: button, attribute: .centerX, relatedBy: .equal, toItem: imageView, attribute: .centerX, multiplier: 1, constant: 0))
button.addConstraint(NSLayoutConstraint(item: button, attribute: .centerY, relatedBy: .equal, toItem: imageView, attribute: .centerY, multiplier: 1, constant: 0))
しかし、私はいつも私のアプリをクラッシュします。
どのように私は中心のボタンを達成できますか?
を試してみてください?私はどのようなビュー要素を意味しますか? – Tajnero
制約を持つすべての要素に対して。 –
これは間違いなく助けになりました。 – Tajnero