2017-10-03 14 views
-1

이미지가 탐색 모음 제목으로 설정된 앱이 있습니다. 나는 완벽한 크기를 가졌지 만, 내 iPhone과 Mac/xcode를 업데이트 한 이래로 이미지는 이미지의 실제 크기가 아니고 설정 한 것이 아닙니다. 이 문제를 어떻게 해결할 수 있습니까? 감사.Swift 3 새로운 업데이트. 내 탐색 제목이 업데이트 전의 크기의 두 배가되는 이유는 무엇입니까?

var titleView : UIImageView 

titleView = UIImageView(frame: CGRect(x: 0, y: 0, width: 32, height: 32)) 

titleView.contentMode = .scaleAspectFit 

titleView.image = UIImage(named: "logo.png") 

self.navigationItem.titleView = titleView 

답변

1

과 같이 사용하십시오 :

var titleView : UIImageView 

titleView = UIImageView(frame: CGRect(x: 0, y: 0, width: 32, height: 32)) 
let widthConstraint = titleView.widthAnchor.constraint(equalToConstant: 32) 
let heightConstraint = titleView.heightAnchor.constraint(equalToConstant: 32) 
heightConstraint.isActive = true 
widthConstraint.isActive = true 
+0

Perfe ct. 고맙습니다. 그냥 크기를 조정해야했습니다. –

0

이 그 일의 약간 다른 방식을 추가하려면 : 자동 레이아웃 사용하는 경우

가 있기 때문에, 그것은 당신의 뷰의 프레임을 설정하는 가치가 없어 레이아웃 패스에서 재정의 될 것이므로 이렇게하는 것이 좋습니다. 덧글은 내가하는 일을 설명하기 위해 추가되었습니다.

// 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