简体   繁体   English

未在TabBarController中嵌入的navigationBar中显示按钮项目

[英]Button item not shown in navigationBar embebed in TabBarController

I'm working on an app where a viewController is embebed in navigationBar and this navigationBar is embebed in TabBarViewController. 我正在开发一个应用程序,其中viewController嵌入在navigationBar中,而这个navigationBar嵌入在TabBarViewController中。

In my app I have 2 different tabs (for the moment), one tab is the "Activity view". 在我的应用中,我有2个不同的标签(目前),其中一个标签是“活动视图”。 This is a viewController with a tableView where list activities. 这是一个带有tableView的listController的viewController。 Every cell will show the same view controller showing detail activity when it's touched. 每个单元格都将显示相同的视图控制器,并在触摸时显示详细信息活动。

My problem is that not showing navigationItem with buttons. 我的问题是没有显示带有按钮的navigationItem。

I'm trying the solution of: Bar button item not shown in navigationBar , because my problem is very similar but it did not work. 我正在尝试以下解决方案: 导航栏上未显示Bar按钮项 ,因为我的问题非常相似,但是没有用。

some screenshots of storyboard and app running: storyboard App running without buttons 故事板和应用程序运行的一些屏幕截图: 故事板 应用程序在没有按钮的情况下运行

I'm trying programmatically add buttons but no results: 我正在尝试以编程方式添加按钮,但没有结果:

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]

Check your image. 检查您的图像。 I don't see any problem with your code. 我看不到您的代码有任何问题。 Or you can try add just title like below 或者您可以尝试仅添加如下标题

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

If button bar show. 如果显示按钮栏。 I sure that problem is your image. 我确定问题出在你的形象上。

I also faced the similar problem, later I found that set up your navigation bar in tab bar controller itself, no need to do in view controllers 我也遇到了类似的问题,后来我发现可以在选项卡栏控制器本身中设置导航栏,而无需在视图控制器中进行设置

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)

For tabar application we should add like this, 对于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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM