skip to Main Content

Why my Navigation and Normal Buttons text and images are not visible but actionable, when i build app on iphone and as well as when i check app after building an app on simulator?

This is how i am adding button to navigation bar:

    @objc func moreNavigationButton() -> UIBarButtonItem {

    let barButton:UIButton = UIButton.init(type: .custom)
    barButton.addTarget(self, action:#selector(moreNavigationButtonClicked), for: .touchUpInside)
    barButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    barButton.setImage(UIImage(named: "ic_more"), for: .normal)
    barButton.imageView?.contentMode = .scaleAspectFit
    
    let barButtonItem:UIBarButtonItem = UIBarButtonItem.init(customView: barButton)
    barButtonItem.width = 30
    
    return barButtonItem
}

In viewWillAppear() function i am calling the above method like following:

    self.navigationItem.rightBarButtonItems = [self.moreNavigationButton()]

3

Answers


  1. Chosen as BEST ANSWER

    The below extension was causing this problem. Now i commented it and it is solved.

    extension UIButton {
        open override func layoutSubviews() {
                super.layoutSubviews()
                if imageView != nil {
                    imageEdgeInsets = UIEdgeInsets(top: 5, left: (bounds.width - 5), bottom: 5, right: 5)
                    titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: (imageView?.frame.width)!)
                }
            }
    }
    

  2. All right now we have different setting up for button in my side I usually do like that

    navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped)) 
    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
    

    then after I set 2 barButtonItem

    let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    let play = UIBarButtonItem(title: "Play", style: .plain, target: self, action: #selector(playTapped))
    

    Here I had them to navigation items
    navigationItem.rightBarButtonItems = [add, play]

    I suppose you need to set Bar button item instead of button.

    Thanks to Paul Hudson content It’s always helpful
    Let me know if it’s helpful

    Login or Signup to reply.
  3. Good morning every one So this morning I create a new project with the system of create your own navigationBar and your own barButton. Feel free to download fork do some pull request or whatever.

    https://github.com/Gioovannii/Navigation-bar/tree/main

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search