Reputation: 1259
I want to get two buttons under status bar:
First, I hided navigationBar:
navigationController?.setNavigationBarHidden(true, animated: false)
and made 2 buttons on the place of NavigationBar. It worked on simulator, but on real device (iPhone 6) touch event didn't work when the buttons were in navigationBar area
I decided to make custom NavigationBar with transparent background and 2 buttons (one instead back btn and second as a rightView)
I tried instructions from Apple doc:
let backButtonBackgroundImage = UIImage(named: "testDpng.png")
let barAppearance =
UINavigationBar.appearance(whenContainedInInstancesOf: [SubViewController.self])
barAppearance.backIndicatorImage = backButtonBackgroundImage
barAppearance.backIndicatorTransitionMaskImage = backButtonBackgroundImage
// Nudge the back UIBarButtonItem image down a bit.
let barButtonAppearance =
UIBarButtonItem.appearance(whenContainedInInstancesOf: [SubViewController.self])
barButtonAppearance.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: -5), for: .default)
But nothing happened. The backBtn looks as usual.
How can I fix it? Is it correct way - replace navigation items? Or I should hide navigation bar as I tried from the beginning?
Upvotes: 0
Views: 709
Reputation: 1775
Don't hide navigationBar, Make it transparent and add UIBarButtonItem
to left and right side of navigationBar.
Below code working for Swift 5 :
override func viewDidLoad() {
super.viewDidLoad()
//Make NavigationBar Transparent
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
//Add Left Button Which Has Image
let leftButton = UIBarButtonItem(image: UIImage(named: "shape"), style: .plain, target: self, action: #selector(crossBtnTapped))
self.navigationItem.leftBarButtonItem = leftButton
//Add Right Button Which Has Title
let rightButton = UIBarButtonItem(title: "Restore", style: .done, target: self, action: #selector(restoreTapped))
rightButton.tintColor = .darkGray
self.navigationItem.rightBarButtonItem = rightButton
}
Action methods on buttons tap
@objc func restoreTapped(){
print("Restore Tapped")
}
@objc func crossBtnTapped(){
print("Cross Tapped")
}
Output :
Upvotes: 1