gpt4 book ai didi

ios - RightBarButtonItem 未显示在正确的位置

转载 作者:行者123 更新时间:2023-11-28 10:14:19 26 4
gpt4 key购买 nike

我正在以编程方式创建 UINavigationController 并将我的 ViewController 添加到其中:

// wrap in a navigation controller
let navigationController = UINavigationController(rootViewController: myViewController)
self.present(navigationController, animated: true, completion: nil)

然后将图片添加到导航项:

let backButton: UIBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: target, action: selector)
backButton.image = UIImage(named: "ic_back")
self.navigationItem.rightBarButtonItem = backButton

但是我的 BarButton 没有显示在正确的位置。我做错了什么?

enter image description here

最佳答案

您需要将 UIBarButton 直接初始化为 UIImage 或自定义 View :

即,

swift 3.0:

let image = UIImage(named: "ic_back")?.withRenderingMode(.alwaysOriginal)
let button = UIBarButtonItem(image: image, style: .plain, target: target, action: selector)

self.navigationItem.rightBarButtonItem = button

这将解决您的问题。

关于ios - RightBarButtonItem 未显示在正确的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43323874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com