iOS 11中的imageView使用Swift

问题描述 投票:1回答:2

我在导航控制器中创建了2个关键按钮。我有一个左键和一个右键,都有一些文字+箭头图标。自从我更新到iOS 11后,图标的大小发生了变化,我不知道为什么。

这就是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
}
ios swift image uibutton ios11
2个回答
2
投票

您需要在xCode 9中为按钮添加宽度约束。像这样:

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

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
© www.soinside.com 2019 - 2024. All rights reserved.