简体   繁体   中英

Why my Navigation and Normal Buttons text and images are not visible but actionable?

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()]

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

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

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)!)
            }
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM