2017-07-27 3 views
0

viewDidLoadには次のようなものがあります。カスタムrightBarButtonItem(カスタムUINavigationBar)を設定します。UIBarButtonItem(customView :) - タップ可能ではありません

let button = UIButton() 
    let attrs = [NSFontAttributeName: UIFont(name: "Montserrat-Light", size: 14)!, NSForegroundColorAttributeName: UIColor.black] 
    let title = NSAttributedString(string: "Sign In", attributes: attrs) 
    button.setAttributedTitle(title, for: .normal) 
    button.addTarget(self, action: #selector(self.pushVcLogin), for: .touchUpInside) 

    let parentView = UIView(frame: button.bounds) 
    parentView.addSubview(button) 
    button.sizeToFit() 
    parentView.bounds = CGRect(x: 0, y: 0, width: button.bounds.size.width, height: button.bounds.size.height) 

    self.navItem.rightBarButtonItem = UIBarButtonItem(customView: parentView) 

カスタムUIBarButtonItemは、見た目には見えますが、タップ可能ではありません。

アイデア?

+0

あなたは "pushVcLogin" のコードを共有することができますか? –

+0

「カスタムUINavigationBar」とはどういう意味ですか?あなたは 'UINavigationController'を使っていますか?視覚的な外観が「カスタム」なのか、スタンドアロンのナビゲーションバーを使用していますか? –

+0

申し訳ありませんが、私はそれを考え出しました - この全体のビューコントローラは、UINavigationControllerにネストされた(cue @ NicolasMiariの質問です)、そのコントローラはUINavigationBarをカスタムUINavigationBarを含む私の全体のビューに重ねました。 –

答えて

1

まず第一に、あなたのビュー階層がUINavigationController

第二に、あなたがnavigationItemを使用し、その上にカスタムUIBarButtonItemを追加することができますを持っている場合。

私はちょうど次のコードでチェックし、それは動作します。ここにも

let button = UIButton()   
    let attrs = [NSFontAttributeName: UIFont(name: "Montserrat-Light", size: 14)!, NSForegroundColorAttributeName: UIColor.black] 
    let title = NSAttributedString(string: "Sign In", attributes: attrs) 
    button.setAttributedTitle(title, for: .normal) 
    button.addTarget(self, action: #selector(self.pushVcLogin), for: .touchUpInside) 

    let parentView = UIView(frame: button.bounds) 
    parentView.addSubview(button) 
    button.sizeToFit() 
    parentView.bounds = CGRect(x: 0, y: 0, width: button.bounds.size.width, height: button.bounds.size.height) 

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: parentView) 

navigationControllerとのViewControllerがどのように見えるかストーリーボードの画像です。

enter image description here

関連する問題