【问题标题】:Button item not shown in navigationBar embebed in TabBarControllerTabBarController 中嵌入的导航栏中未显示按钮项
【发布时间】:2016-03-31 14:14:13
【问题描述】:

我正在开发一个应用程序,其中 viewController 嵌入在 navigationBar 中,并且此 navigationBar 嵌入在 TabBarViewController 中。

在我的应用程序中,我有 2 个不同的选项卡(目前),一个选项卡是“活动视图”。这是一个带有 tableView 的 viewController,其中列出了活动。每个单元格都将显示相同的视图控制器,当它被触摸时显示详细活动。

我的问题是没有显示带有按钮的 navigationItem。

我正在尝试Bar button item not shown in navigationBar 的解决方案,因为我的问题非常相似,但没有奏效。

故事板和应用程序运行的一些屏幕截图: storyboard App running without buttons

我正在尝试以编程方式添加按钮,但没有结果:

var logoutButton: UIBarButtonItem!
var filterButton: UIBarButtonItem!


//MARK: - Lyfe cycle
override func viewDidLoad() {
super.viewDidLoad()

let logoutImage = UIImage(named: "logout-24")
let filterImage = UIImage(named: "filterOutline-24")

logoutButton = UIBarButtonItem(image: logoutImage, style: .Plain, target: self, action: "logoutAction:")
filterButton = UIBarButtonItem(image: filterImage, style: .Plain, target: self, action: "filterAction:")
navigationItem.rightBarButtonItems = [filterButton, logoutButton]

【问题讨论】:

  • 我尝试了几种方法将 SPLITVIEW 用作侧边菜单,但均未成功。所以我寻找其他方法来执行所需的结果。所以我找到了一个侧面菜单stackoverflow github github,很快就得到了预期的结果。包括tabbar及其各自的航次。还是谢谢你。

标签: ios iphone swift navigationbar


【解决方案1】:

对于tabar应用,我们应该像这样添加,

 let logoutButton = UIBarButtonItem(image: UIImage(named: "ic_logout"), style: .plain, target: self, action:#selector(logoutButtonTapped))    
 self.tabBarController?.navigationItem.leftBarButtonItem = logoutButton

 let nightModeButton = UIBarButtonItem(image: UIImage(named: "ic_night_mode_off"), style: .plain, target: self, action:#selector(nightModeButoonTapped))
 self.tabBarController?.navigationItem.rightBarButtonItem  = nightModeButton

【讨论】:

    【解决方案2】:

    检查您的图像。我认为您的代码没有任何问题。或者您可以尝试添加如下所示的标题

    logoutButton = UIBarButtonItem(title: "Log out", style: .Plain, target: self, action:Selector("logoutAction:"))
    filterButton = UIBarButtonItem(title: "Filter", style: .Plain, target: self, action: Selector("filterAction:"))
    

    如果按钮栏显示。我确定问题出在你的形象上。

    【讨论】:

    • 谢谢,但它不起作用。该按钮仍未显示。我认为这与navigationItem 未显示有关,但我不知道如何解决它...
    • 以前的版本(没有标签栏)显示按钮,但是当我添加标签栏时,这个按钮没有显示...linklink。我需要为其他视图添加 tabBar...:/
    • 你能检查你的navigationItem它是否为零吗?
    • 有趣...我如何检查我的 navigationItem 它为零?我的 viewDidLoad 的一部分:override func viewDidLoad() { super.viewDidLoad() activityIndicator.hidesWhenStopped = true if let contract = detailContract { navigationItem.title = contract.Name loadActivities(contract) } }
    • 您在 viewdidload 中打印 print(navigationItem)。
    【解决方案3】:

    我也遇到了类似的问题,后来发现你的导航栏设置在tab bar控制器本身,不需要在view controller里面做

    let profilePutton = UIButton(type: .custom)
    profilePutton.setTitle("Profile", for: .normal)
    profilePutton.frame = CGRect(x: 0.0, y: 0.0, width: 30.0, height: 30.0)
    profilePutton.addTarget(self, action: #selector(openProfilePage), for: .touchUpInside)
    navigationItem.rightBarButtonItem = UIBarButtonItem(customView: profilePutton)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-11
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多