【问题标题】:How to display buttons on navigation bar in UIPageViewController?如何在 UIPageViewController 的导航栏上显示按钮?
【发布时间】:2016-12-07 00:07:41
【问题描述】:

我使用连接到UINavigationControllerUIPageViewController 来显示两个页面,一个用户表和一个聊天表。

我正在尝试实现以下内容:

UsersChat 两个按钮,点击后会出现相应的视图控制器。我想要导航栏上的这些按钮。

这是我在页面视图控制器类中编写的代码

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, self.view.frame.width/2, 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)

很遗憾,导航栏中没有显示任何按钮。 事实上,我什至无法为导航栏指定标题。我该如何解决这个问题?

谢谢

【问题讨论】:

    标签: ios swift uinavigationcontroller uipageviewcontroller


    【解决方案1】:

    您可以像这样在导航栏右侧添加多个按钮

    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,  style: .Plain, target: self, action: "didTapSearchButton:")
    
    navigationItem.rightBarButtonItems = [editButton, searchButton]
    

    self.navigationController?.navigationBar?.navigationItem.rightBarButtonItems = [editButton, searchButton]
    

    如果你想让它看起来在导航栏的中心,你 可以使用适当宽度的图像,使其向中心缩放。

    【讨论】:

    • 感谢您的回复。不幸的是,这并不能解决问题。我转移到 RKSwipeViewController 以获取内置功能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 2013-01-20
    • 2019-01-11
    • 1970-01-01
    相关资源
    最近更新 更多