Alexey K
Alexey K

Reputation: 6723

Custom button in navigation bar - added and gets tapped but not visible

I try to add custom back button in navigation controller. I create button like this

    func setupBackButton() {
    let backButton = UIButton.init(type: .custom)
    backButton.addTarget(self, action: #selector(backButtonHandler(_:)), for: .touchUpInside)
    backButton.setTitleColor(UIColor.white, for: .normal)
    backButton.setTitle("<", for: .normal)
    backButton.tintColor = UIColor.white
    let barBackButton = UIBarButtonItem(customView: backButton)
    self.navigationItem.leftBarButtonItem  = barBackButton
}

The problem is that button is being added (I can tap it and action on button is being executed) but i can't see button in controller - it is invisible.

Controller's color is blue.

Upvotes: 0

Views: 47

Answers (2)

MohyG
MohyG

Reputation: 1348

try this :

func setupBackButton(){
    let backBtn : UIBarButtonItem = UIBarButtonItem(title: "<", style: UIBarButtonItemStyle.plain, target: self, action:#selector(backToPreviousVC))
    self.navigationItem.leftBarButtonItem = backBtn
    backBtn.tintColor = UIColor.white
}

func backToPreviousVC() {
    self.navigationController?.popViewController(animated: true)
}

call setupBackButton() in viewDidLoad

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

you missed the frame of your button

backButton.frame = CGRect(x: 0, y: 0, width: 44, height: 44)

dont forget to add the action handler

func backButtonHandler(_ sender : UIButton)  {

}

Upvotes: 1

Related Questions