2016-01-26 59 views
1

我使用的是splitViewController,使我的應用程序彈出菜單幻燈片,用斯威夫特在iOS 9故障使用自定義圖像splitViewController displayModeButtonItem(的UIBarButtonItem)斯威夫特IOS 9

我所擁有的一切工作根據需要,除了我無法獲得我的「菜單」按鈕(這是splitViewController的displayModeButtonItem)的自定義圖像。

這是我試過的。

override func viewDidLayoutSubviews() { 

    // This will create a properly-working menu barbuttonitem, but is of course 
    // a text based title. 

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "≡", 
    style: UIBarButtonItemStyle.Plain, 
    target: splitViewController!.displayModeButtonItem().target, 
    action: splitViewController!.displayModeButtonItem().action) 

此版本是我嘗試使用自定義圖像而不是文本標題,但我只是在圖像應該是灰色框。

navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "29x29"), 
    style: .Plain, 
    target: splitViewController!.displayModeButtonItem().target, 
    action: splitViewController!.displayModeButtonItem().action) 

有什麼建議嗎?
感謝

回答

1

你想使一個UIButton並設置它的形象,並設置爲leftBarButtonItem像這樣在你的-viewDidLoad

let menuButton = UIButton(type: .Custom) 
menuButton.frame = CGRectMake(0, 0, 29, 29) 
menuButton.setImage(UIImage(named:"29x29"), forState: .Normal) 
menuButton.addTarget(self, action: "menuPressed:", forControlEvents: .TouchUpInside) 
let barButton = UIBarButtonItem(customView: menuButton) 
navigationItem.leftBarButtonItem = barButton 

,並在一個單獨的方法,執行你的行動:

func menuPressed(sender: AnyObject) { 
    //show menu 
} 
+0

謝謝@jasonnoahchoi,你的解決方案解決了我的barbuttonitem圖像問題!我唯一沒有想到的是如何繼續利用splitViewController!.displayModeButtonItem()動作和目標。雖然我知道如何切換splitViewController.preferredDisplayMode,但我似乎無法保留我的Menu的動畫顯示/隱藏... – bayarea

+1

好吧,我現在明白了。觸發內置的splitViewController!.displayModeButtonItem()動作我在調用:UIApplication.sharedApplication()。sendAction(splitViewController!.displayModeButtonItem()。action,to:splitViewController!.displayModeButtonItem()。target,from:nil,forEvent :無)。非常感謝! – bayarea

+0

不錯!非常高興你能弄明白。 – jasonnoahchoi