iosbegindevel
iosbegindevel

Reputation: 327

how to adjust the position of the UINavigationBar in swift5?

I would like to reposition the UINavigationBar. So I used the code to give a new value, but it hasn't changed.

@IBOutlet var headerBar: UINavigationBar!
let screenHeight = UIScreen.main.bounds.size.height
    override func viewDidLoad() {
        super.viewDidLoad()
        if screenHeight > 667 {
            headerBar = UINavigationBar(frame: CGRect(x: 0, y: 54, width: headerBar.frame.width, height: headerBar.frame.height))
        } else {
            headerBar = UINavigationBar(frame: CGRect(x: 0, y: 40, width: headerBar.frame.width, height: headerBar.frame.height))
        }
    }

How can I solve this problem?

I ran on the main thread but there was no change.

@IBOutlet var headerBar: UINavigationBar!
let screenHeight = UIScreen.main.bounds.size.height
    override func viewDidLoad() {
        super.viewDidLoad()
        DispatchQueue.main.async {
            if self.screenHeight > 667 {
                self.headerBar = UINavigationBar(frame: CGRect(x: 0, y: 54, width: self.headerBar.frame.width, height: self.headerBar.frame.height))
            } else {
                self.headerBar = UINavigationBar(frame: CGRect(x: 0, y: 40, width: self.headerBar.frame.width, height: self.headerBar.frame.height))
            }
        }
    }

Headerbar Area Viewed from Main Storyboard

NOTE: This header bar is obscured by the navigation controller bar in the picture. But I'm hiding the navigation controller bar in the code. enter image description here

Upvotes: 0

Views: 47

Answers (1)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

try this

    override func viewDidLoad() {
        super.viewDidLoad()
        DispatchQueue.main.async {
             let screenHeight = UIScreen.main.bounds.size.height
             let bounds = self.headerBar.bounds
           self.headerBar.frame = CGRect(x: 0, y: self.screenHeight > 667 ? 54 : 40, width:bounds.width, height: bounds.height )

        }
    }

Upvotes: 1

Related Questions