我正在使用连接到UINavigationController的UIPageViewController来显示两个页面,一个用户表和一个聊天表.
我正在尝试实现以下内容:
两个按钮用户和聊天点击时会出现相应的视图控制器.我想在导航栏上显示这些按钮.
viewDidLoad() let storyBoard = UIStoryboard(name: "Main",bundle: nil) let vc1 = storyBoard.instantiateViewControllerWithIdentifier("users") let vc2 = storyBoard.instantiateViewControllerWithIdentifier("chat") vcArray = [vc1,vc2] self.setViewControllers([vc1],direction: .Forward,animated: true,completion: nil) let button1 = UIButton(frame: CGRectMake(0,30,self.view.frame.width/2,30)) let button2 = UIButton(frame: CGRectMake(self.view.frame.width/2+1,30)) button1.setTitle("Users",forState: .Normal) button2.setTitle("Chat",forState: .Normal) self.navigationController?.navigationBar.topItem?.titleView?.addSubview(button1) self.navigationController?.navigationBar.topItem?.titleView?.addSubview(button2)
不幸的是,导航栏中没有任何按钮.
事实上,我甚至无法为导航栏指定标题.我怎样才能解决这个问题?
谢谢
解决方法
您可以在导航栏的右侧添加多个按钮,如下所示
let editImage = UIImage(named: "First")! let searchImage = UIImage(named: "Second")! let editButton = UIBarButtonItem(image: editImage,style: .Plain,target: self,action: "didTapEditButton:") let searchButton = UIBarButtonItem(image: searchImage,action: "didTapSearchButton:") navigationItem.rightBarButtonItems = [editButton,searchButton]
要么
self.navigationController?.navigationBar?.navigationItem.rightBarButtonItems = [editButton,searchButton]
If you want to make it look in the centre on the navigation bar,you can use image with appropriate width so that it scales towards the center.