Reputation: 327
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?
@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))
}
}
}
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.
Upvotes: 0
Views: 47
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