簡體   English   中英

使用 Swift 在 iOS 11 中查看圖像

[英]imageView in iOS 11 using Swift

我在導航控制器中創建了 2 個自定義按鈕。 我有一個左側按鈕和一個右側按鈕,並且都有一些文本 + 一個箭頭圖標。 自從我更新到 iOS 11 后,圖標的大小發生了變化,我不知道為什么。

這是 iOS 10(左)和 iOS 11(右)之間的區別:

在此處輸入圖片說明

我怎樣才能改變它?

這是我的一段代碼:

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
}

您需要在xCode 9中為按鈕添加寬度約束。像這樣:

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

我認為您可以為圖像視圖和按鈕的高度錨和寬度錨設置約束。

嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM