2016-08-01 80 views
3

我使用連接到UINavigationControllerUIPageViewController來顯示兩個頁面,一個用戶表和一個聊天表。如何在UIPageViewController的導航欄上顯示按鈕?

我想實現以下:

兩個按鈕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) 

不幸的是,所有按鈕都出現在導航欄的代碼。 事實上,我甚至不能爲導航欄分配標題。我怎樣才能解決這個問題?

感謝

回答

0

您可以在導航欄的右側添加多個按鈕,這樣

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] 

如果你想使它看起來在中心在導航欄上,您可以使用具有適當寬度的圖像,使其朝中心縮放。

+0

感謝您的回覆。不幸的是,這並不能解決問題。我轉移到RKSwipeViewController的內置功能 –