I've implemented a customised back button in swift 4 but the button doesn't show. I can see it in the layer that it's there but it's on the back and can't bring it to the front. Here is my code in viewDidload:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
collectionView?.translatesAutoresizingMaskIntoConstraints = false
collectionView?.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 58, right: 0)
collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
collectionView?.alwaysBounceVertical = true
collectionView?.backgroundColor = UIColor(patternImage: UIImage(named: "chat-bg")!)
collectionView?.register(ChatMessageCell.self, forCellWithReuseIdentifier: cellId)
collectionView?.keyboardDismissMode = .interactive
let username = Auth.auth().currentUser?.displayName
let backbutton = UIButton(type: .custom)
backbutton.translatesAutoresizingMaskIntoConstraints = false
backbutton.setImage(UIImage(named: "back"), for: .normal) // Image can be downloaded from here below link
backbutton.setTitle(username, for: .normal)
backbutton.setTitleColor(backbutton.tintColor, for: .normal) // You can change the TitleColor
backbutton.addTarget(self, action: #selector(backAction), for: .touchUpInside)
self.navigationController?.navigationItem.backBarButtonItem = UIBarButtonItem(customView: backbutton)
self.navigationController?.navigationBar.addSubview(backbutton)
setupInputComponents()
setupKeyboardObservers()
}
Also, attached an screenshot of the layout
What I'm missing? any help appreciated.
I finally fixed it. The issue wasn't related to this part of my code. It was related to another controller which was a CustomTabController and contains the navigationBar. I wasn't able to override that by this code. So, I moved the navigationBar section outside of the CustomBarController and that fixed the issue. This answer is very specific to my project and just shared it to let readers know that it's fixed in a different way.