使用 Xcode 9.3 版 Swift 开发 iOS 应用程序。
导航左栏按钮不出现。你能告诉我如何显示左栏按钮吗?
当我用相同的代码尝试右侧的按钮时,它显示没有任何问题。
代码和 Storyboard如下。
第一个TableView代码
import UIKit
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
let naviButton = UIBarButtonItem(title: "naviButton", style: UIBarButtonItemStyle.plain, target: self, action: #selector(Tapped))
// Left bar button does not appear
self.navigationItem.leftBarButtonItem = naviButton
// Right bar button do appear
self.navigationItem.rightBarButtonItem = naviButton
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
@objc func Tapped() {
print("Tapped")
}
...
Storyboard
模拟器
最佳答案
You can not place same button twice in
UINavigationBar
. So create 2 separate button and then assign them toleftbarButton
andrightbarButton
:
override func viewDidLoad() {
super.viewDidLoad()
let leftNaviButton = UIBarButtonItem(title: "lNaviButton", style: UIBarButtonItemStyle.plain, target: self, action: #selector(Tapped1))
self.navigationItem.leftBarButtonItem = leftNaviButton
let rightNaviButton = UIBarButtonItem(title: "rNaviButton", style: UIBarButtonItemStyle.plain, target: self, action: #selector(Tapped2))
self.navigationItem.rightBarButtonItem = rightNaviButton
}
@objc func Tapped1() {
print("Tapped1")
}
@objc func Tapped2() {
print("Tapped2")
}
关于ios - 如何显示导航左栏按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49654754/