2017-01-02 7 views
0

ナビゲーションバーの右に2つのボタンがあります。BGカラーとテキストスイフトを使用したカスタムUIBarButtonItem 3

extension UIViewController { 

func setNavigationBarItem() { 

    let profileButton = UIBarButtonItem(title: "?", style: .plain, target: self, action: #selector(didTapProfileButton(_:))) 
    navigationItem.rightBarButtonItems = [addRightBarButtonWithImage(UIImage(named: "menu")!), profileButton] 

    self.slideMenuController()?.removeRightGestures() 
    self.slideMenuController()?.addRightGestures() 
    } 
} 

私はこのように2つのボタンを作成しました。しかし、私が望むprofileButtonは、背景色とそのボタンの角の半径を持っています。それのように見えるように、それを追加する方法:

enter image description here

は黒い部分を無視します。 UIBarButtonは黄色の背景色とコーナー半径になります。そのために

+0

あなたの実際の期待とあなたが得ているものは、plz指定してください。 –

答えて

4

あなたはそれがそのボタンからUIBarButtonItemインスタンスを作成した後、あなたが望むデザインでUIButtonインスタンスを作成する必要があるオプションを1つだけ持っています。

let btnProfile = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25)) 
btnProfile.setTitle("SY", for: .normal) 
btnProfile.backgroundColor = UIColor.yellow 
btnProfile.layer.cornerRadius = 4.0 
btnProfile.layer.masksToBounds = true 

今そのボタンからUIBarButtonItemを作成し、rightBarButtonItemsでそれを設定します。

navigationItem.rightBarButtonItems = [addRightBarButtonWithImage(UIImage(named: "menu")!), UIBarButtonItem(customView: btnProfile)] 
+0

ありがとうございます。それは私の要求に従って動作します。 – Nitesh

+0

@Niteshウェルカムメイト、うれしいです:) –

2
let profileButton = UIButton() 
    profileButton.frame = CGRect(x:0, y:0, width:30, height:30) 
    profileButton.setTitle("SY", for: .normal) 
    profileButton.setTitle("SY", for: .highlighted) 
    profileButton.backgroundColor = UIColor.yellow 
    profileButton.layer.cornerRadius = 5.0 
    profileButton.addTarget(self, action: #selector(didTapProfileButton(_:)), for: .touchUpInside) 

    let rightBarButton = UIBarButtonItem(customView: profileButton) 
    self.navigationItem.rightBarButtonItem = rightBarButton 
+0

答えが間違って更新されました。私はすでにそれをロールバックしています。 –

+0

ありがとうございます。それは私の要求に従って動作します。 – Nitesh

関連する問題