ios - 如何显示导航左栏按钮?

标签 ios swift uinavigationbar

使用 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

Storyboard

模拟器

enter image description here

最佳答案

You can not place same button twice in UINavigationBar. So create 2 separate button and then assign them to leftbarButton and rightbarButton:

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/

相关文章:

ios - 在 objective-c 中逐行读取 NSData

ios - 需要对 URL 进行 NSString 编码。 CFURLCreateStringByAddingPercentEscapes 破坏了我的字符串

ios - 在表格 View 中显示多个单元格时出现问题

ios - iOS6 中 UINavigationBar 的顶角被圆化

ios - 将 2 个 ViewController 组合在 TabBarView 的单个选项卡中,并允许在同一选项卡内来回切换

ios - Interface Builder 裁剪可设计的 View

swift - 如何知道 SKAction.repeat(action, count) 何时完成

ios - 没有街道和名字的 MapKit

ios - 后退按钮没有相应的颜色( objective-c )

ios - 使 UINavigationBar 透明