为什么导航栏按钮在iOS10中消失了?

时间:2018-08-31 18:54:58

标签: ios swift4 navbar

创建按钮的代码:

languageTopBarButton = UIButton(type: .custom)
languageTopBarButton.setImage(UIImage(named: "iconGlobe"), for: .normal)
languageTopBarButton.setTitle(title, for: .normal)
languageTopBarButton.setTitleColor(UIColor.green, for: .normal)
languageTopBarButton.addTarget(self, action: #selector(rightNavButtonTapped), for: .touchUpInside)

let navBarHeight = navigationController?.navigationBar.frame.size.height
var rightButtonFrame = languageTopBarButton.frame
rightButtonFrame.size = CGSize(width: rightButtonFrame.width, height: navBarHeight!)
languageTopBarButton.frame = rightButtonFrame

let buttonItem = UIBarButtonItem(customView: languageTopBarButton)

navigationItem.rightBarButtonItem = buttonItem

该按钮在iOS11中确定,但在iOS10中消失

enter image description here

怎么了?

1 个答案:

答案 0 :(得分:1)

我尝试创建一个按钮。在iOS10和iOS11上运作良好。我认为您代码中的问题是您为自定义按钮设置了错误的框架。

override func viewDidLoad() {
    super.viewDidLoad()

    setupBarButton()
}

private func setupBarButton() {
    let languageTopBarButton = UIButton(type: .custom)
    languageTopBarButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    languageTopBarButton.setImage(UIImage(named: "iconGlobe"), for: .normal)
    let rightBarButton = UIBarButtonItem(customView: languageTopBarButton)
    self.navigationItem.rightBarButtonItem = rightBarButton
}