iOS 11 中的 imageView 使用 Swift

imageView in iOS 11 using Swift

我在导航控制器中创建了 2 个自定义按钮。我有一个左按钮和一个右按钮,并且都有一些文本 + 一个箭头图标。自从我更新到iOS11后,图标的大小变了,不知道为什么。

这是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