Ios 如何在没有UINavigationController的情况下向导航栏添加多个按钮?
要在UINavigationController中向viewController添加多个按钮, 我只想说这句话:Ios 如何在没有UINavigationController的情况下向导航栏添加多个按钮?,ios,swift,uinavigationbar,ios9,uibarbuttonitem,Ios,Swift,Uinavigationbar,Ios9,Uibarbuttonitem,要在UINavigationController中向viewController添加多个按钮, 我只想说这句话: self.navigationItem.rightBarButtonItems = [my button] 但我无法成功地将其添加到没有UINavigationController的视图中: //create new navigation item. and add it to the nvigation bar customNavigationItem = UI
self.navigationItem.rightBarButtonItems = [my button]
但我无法成功地将其添加到没有UINavigationController的视图中:
//create new navigation item. and add it to the nvigation bar
customNavigationItem = UINavigationItem()
/* now we need to add the menu + search to the navigation item */
//create search button
let searchButton = UIButton();
searchButton.frame = CGRectMake(0, 0, 50.0, 50.0);
searchButton.backgroundColor = UIColor.redColor();
searchButton.titleLabel!.font = UIFont.iconmoonFont(14.0)
searchButton.titleLabel?.textColor = UIColor.whiteColor()
searchButton.titleLabel!.text = IconsConstants.SEARCH;//SEARCH ICON TEXT
searchButton.backgroundColor = UIColor.clearColor();
searchButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center;
searchButton.contentVerticalAlignment = UIControlContentVerticalAlignment.Center;
let searchUIbarBtn = UIBarButtonItem.init(customView: searchButton)
//create menu button
let menuButton = UIButton();
menuButton.frame = CGRectMake(0, 0, 40.0, 40.0);
menuButton.titleLabel!.font = UIFont.iconmoonFont(14.0)
menuButton.backgroundColor = UIColor.whiteColor()
menuButton.titleLabel!.text = IconsConstants.MENU;//SEARCH ICON TEXT
menuButton.backgroundColor = UIColor.clearColor();
menuButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center;
menuButton.contentVerticalAlignment = UIControlContentVerticalAlignment.Center;
let menuUIbarBtn = UIBarButtonItem.init(customView: menuButton)
//add button to NavigationItem
customNavigationItem.rightBarButtonItems = [menuUIbarBtn,searchUIbarBtn];
/* add navigation bar */
navigationBar = UINavigationBar();
navigationBar.frame = CGRectMake(0, statusBarHeight, self.view.frame.size.width, 44);
//navigationBar.translucent = false
navigationBar.barTintColor = UIColor.statusBarColor()
navigationBar.items = [customNavigationItem]
self.view.addSubview(navigationBar)
我不明白为什么我得到的是一个没有任何按钮的栏?在将按钮提供给
UINavigationBar
之前,而不是之后,您是否尝试过将按钮添加到UINavigationItem
?另外,您的代码没有显示您的UINavigationBar
被添加到您的视图中。@jcaron谢谢您的评论,我已经编辑了代码,这没有帮助!