ios - 无法更改 iOS 11 中的导航栏项目高度

标签 ios swift uinavigationbar

自定义导航栏高度大于默认值(44pt)后,我想更改右侧导航栏项目按钮的高度,但限制在44pt。我怎样才能让它长得更高?我知道在 iOS 11 中,按钮现在位于 UIBarButtonStackView 中,似乎我们无法更改堆栈 View 框架?

我使用这段代码来改变按钮的宽度和高度:

button.widthAnchor.constraint(equalToConstant: 40).isActive = true
button.heightAnchor.constraint(equalToConstant: 60).isActive = true
button.translatesAutoresizingMaskIntoConstraints = false
button.setImage(image, for: .normal)

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

谢谢!

最佳答案

您可以使用此代码更改导航栏按钮项的宽度 -

override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            var frame: CGRect? = navigationItem.leftBarButtonItem?.customView?.frame
            frame?.size.width = 5  // change the width of your item bar button
            self.navigationItem.leftBarButtonItem?.customView?.frame = frame!
        }
        override var prefersStatusBarHidden : Bool {
            return true
        }

或从 Storyboard -

enter image description here

确保您的 Assets.xcassets 图像设置为 Render As - Original Image Just like -

enter image description here

关于ios - 无法更改 iOS 11 中的导航栏项目高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49766349/

相关文章:

ios - 谷歌地图无法加载,整个应用程序崩溃

ios - 如何在 XCUI 测试自动化框架中组织和分组测试用例

swift - iOS8 键盘扩展 clipsToBounds

ios - 如何在 Swift 中立即隐藏导航栏?

ios - uisearchbar 导航栏颜色&状态颜色

iOS - UITableViewCell 中 UIButton 的延迟 "Touch Down"事件

ios - UIPrintInteractionController (Swift) 最多可以执行多少次打印?

ios - 导航栏改变高度

ios - Apple 是否为没有 Mac 设备的程序员提供开发 iOS 安装程序?

ios - 横向和纵向的动态角半径