0

I want to get two buttons under status bar: enter image description here

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?

1
  • if you hided your navbar ? then why not have UIView to add buttons ? Commented May 8, 2020 at 12:35

1 Answer 1

1

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 :

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

It helped, but I got the dagger not in the left side of the screen. It seems invisible back button is an obstacle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.