【问题标题】:Custom bar button item not sizing properly自定义栏按钮项目大小不正确
【发布时间】:2017-10-03 02:46:20
【问题描述】:

我有一个自定义应用程序,其中包含一个自定义栏按钮项,在 Swift 3 中它的大小已适当,但在更新到 Swift 4 后,它不再根据所提供的约束调整大小。这是我的代码:

let infoButton = UIButton.init(type: .custom)
infoButton.setImage(UIImage(named: "info button white.png"), for: UIControlState.normal)
infoButton.addTarget(self, action: #selector(StartViewController.infoButtonPressed), for: UIControlEvents.touchUpInside)
infoButton.frame = CGRect(x: 0, y: 0, width: 25, height: 25)

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

我尝试更改 CGRect 数字,看看是否有任何变化,但没有,它的大小已达到导航栏的限制,坦率地说现在看起来很丑。

关于 Swift 4 中发生了什么变化的任何想法?我正在运行 Xcode 9.0(9A235)

【问题讨论】:

  • 问题可能出在 iOS 11 上。我在另一部运行 iOS 10.3.2(14F89) 的手机上运行了该应用程序,并且信息按钮的大小合适,在我的第一台运行 iOS 11.0.1 的设备上运行(15A402) 我明白了。

标签: ios swift uinavigationbar


【解决方案1】:

尝试像这样设置约束:

infoButton.widthAnchor.constraint(equalToConstant: 25).isActive = true
infoButton.heightAnchor.constraint(equalToConstant: 25).isActive = true

或者使用正确尺寸的图片。

【讨论】:

  • 嗨@user1698875,如果这个或任何答案已经解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己提供了一些声誉。没有义务这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多