2017-10-17 134 views
-1

我有頂部導航欄一個UIViewController。我想用分段控件替換標題。添加segmentedcontrol到導航欄在一個視圖控制器

class AViewController: UIViewController 
{ 
    private var navigationbar = UINavigationBar(); 
    private var segment = UISegmentedControl(); 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

     buildThebar(); 
    } 

    func buildThebar() 
    { 
     navigationbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 64)); 

     navigationbar.backgroundColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0); 

     segment = UISegmentedControl(items: ["Testy", "Tests"]); 
     segment.sizeToFit(); 
     segment.tintColor = UIColor(red: 104/255, green: 90/255, blue: 132/255, alpha: 1.0); 

     let cancel = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelbar(sender:))); 

     cancel.tintColor = UIColor(red: 104/255, green: 90/255, blue: 132/255, alpha: 1.0); 

     let title = UINavigationItem(title: "Testing"); 

     title.leftBarButtonItem = cancel; 

     navigationbar.setItems([title], animated: false); 

     self.navigationItem.titleView = segment; 
     // self.navigationItem.leftBarButtonItem = cancel; 

     self.view.addSubview(navigationbar); 
    } 
+0

什麼問題?代碼對我來說工作正常 –

+0

你能發佈結果的圖片嗎?導航欄是否顯示? –

+0

@cdub:看看我發佈的答案 –

回答

0

好的發現問題。

您錯過的是將titleView設置爲您創建的導航項目。

title.titleView = segment 

你加入titleview的導航項目self.navigationItem.titleView = segment;但你需要將其設置爲你的navigationItem它要傳遞到您的導航欄。

最終代碼

func buildThebar() 
{ 

    navigationbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 64)); 

    navigationbar.backgroundColor = UIColor(red: 200/255, green: 200/255, blue: 200/255, alpha: 1.0); 

    segment = UISegmentedControl(items: ["Testy", "Tests"]); 
    segment.sizeToFit(); 
    segment.tintColor = UIColor(red: 104/255, green: 90/255, blue: 132/255, alpha: 1.0); 

    let cancel = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: nil); 

    cancel.tintColor = UIColor(red: 104/255, green: 90/255, blue: 132/255, alpha: 1.0); 

    let title = UINavigationItem(title: "Testing"); //better let title = UINavigationItem() 
    title.leftBarButtonItem = cancel; 
    title.titleView = segment 
    navigationbar.setItems([title], animated: false); 

    // self.navigationItem.leftBarButtonItem = cancel; 

    self.view.addSubview(navigationbar); 
} 

編輯:

有一個與你是如何設置導航欄架的另一個問題。 O /你的代碼爲P看起來像

我相信那不是你想要的!爲什麼它看起來像的原因是因爲你的框架從y = 0啓動,但在iPhone和iPad波泰特模式有基於各種條件消耗的空間變化量在頂部的狀態欄(通常人們硬編碼或承擔它是20,但iPhone X高度狀態欄可能會有所不同,甚至在正常iPhone中,當您收到呼叫狀態欄高度更改時)。它總是更好地使用自動佈局和安全邊距來添加導航欄到視圖。

但如果你真的想硬編碼的幀,那麼你可以使用

navigationbar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: self.view.bounds.width, height: 44)); 

O/P樣子

enter image description here

希望它能幫助。

0
let title = UINavigationItem() 
     title.titleView = segment 
     title.rightBarButtonItem = cancel 
navigationbar.setItems([title], animated: false); 
self.view.addSubview(navigationbar); 

只是在操作中的對象分配順序的問題。

相關問題