이 그 일의 약간 다른 방식을 추가하려면 : 자동 레이아웃 사용하는 경우
가 있기 때문에, 그것은 당신의 뷰의 프레임을 설정하는 가치가 없어 레이아웃 패스에서 재정의 될 것이므로 이렇게하는 것이 좋습니다. 덧글은 내가하는 일을 설명하기 위해 추가되었습니다.
// Unless you are going to recreate the view, just use a let not a var.
// A UIImageView is a reference type, so you can still change the image to be displayed.
// Also, there is no point declaring a variable and then setting it on the next line, just do it all at once.
// Using the non-parameterised initialiser uses a zero frame for the rect.
let titleView = UIImageView()
// Since the view is being created in code and autolayout is going to be applied, you need to add this line to prevent layout conflicts.
titleView.translatesAutoresizingMaskIntoConstraints = false
// Configure the aspect ratio of the displayed image.
titleView.contentMode = .scaleAspectFit
// You don't need to keep a reference to the constraint unless you want to activate and deactivate it.
titleView.widthAnchor.constraint(equalToConstant: 32).isActive = true
// Now, since you want the image to be a square, you can create an layout anchor that specifies this requirement, rather than just duplicating the width value.
titleView.heightAnchor.constraint(equalTo: titleView.widthAnchor, multiplier: 1).isActive = true
Perfe ct. 고맙습니다. 그냥 크기를 조정해야했습니다. –