2017-09-25 63 views
1

我在我的導航控制器中創建了2個海關按鈕。我有一個左鍵和一個右鍵,並且都有一些文本+箭頭圖標。由於我更新到iOS 11,圖標的大小已經改變,我不知道爲什麼。使用Swift的iOS 11中的imageView

這個iOS的10(左)和iOS 11(右)的區別:

enter image description here

我怎樣才能改變這種狀況?

這一塊我的代碼:

func addRightButton(){ 
    rightButton = UIButton.init(type: .custom) 
    rightButton.setImage(UIImage(named: "back"), for: .normal) 
    rightButton.imageView?.contentMode = .scaleAspectFit 

    let width = UIScreen.main.bounds.size.width 
    if width < 375 { 
    rightButton.titleLabel?.font = UIFont.systemFont(ofSize: 13, weight: UIFont.Weight.bold) 
    rightButton.frame = CGRect.init(x: 0, y: 0, width: 75, height: 10) 
    } else { 
    rightButton.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.bold) 
    rightButton.frame = CGRect.init(x: 0, y: 0, width: 100, height: 10) 
    } 

    rightButton.imageView!.transform = rightButton.imageView!.transform.rotated(by: CGFloat((Double.pi/2) * -1)) 
    rightButton.setTitle(" Mixte",for: .normal) 
    rightButton.addTarget(self, action: #selector(self.switchSex(_:)), for: UIControlEvents.touchUpInside) 

    let barButton = UIBarButtonItem(customView: rightButton) 
    self.navigationItem.rightBarButtonItem = barButton 
} 

回答

2

您需要在Xcode 9.添加寬度約束爲您的按鈕像這樣:

let width = yourButton.widthAnchor.constraint(equalToConstant: 75) 
let height = yourButton.heightAnchor.constraint(equalToConstant: 10) 
heightConstraint.isActive = true 
widthConstraint.isActive = true 
+0

這就是我所做的只是現在。但似乎難以集中按鈕。 (超過iOS 10) – KevinB

0

我認爲你可以玩的設置圖像視圖和按鈕的高度錨點和寬度錨點的約束。

嘗試:

button.heightAnchor.constraint(equalTo: (button.imageView?.heightAnchor)!, multiplier: 1, constant: 5).isActive = true 
button.widthAnchor.constraint(equalTo: (button.imageView?.widthAnchor)!, multiplier: 1, constant: 70).isActive = true