I am trying to add custom view to NavigationBar and result not working correct.
public let statusBarHeight: CGFloat = {
var heightToReturn: CGFloat = 0.0
for window in UIApplication.shared.windows {
if let height = window.windowScene?.statusBarManager?.statusBarFrame.height, height > heightToReturn {
heightToReturn = height
}
}
return heightToReturn
}()
override func viewDidLoad() {
super.viewDidLoad()
// self.navigationController?.navigationBar.isHidden = true
// Do any additional setup after loading the view.
let barView = UIView(frame: CGRect.zero)
barView.backgroundColor = .purple
barView.translatesAutoresizingMaskIntoConstraints = false
if let navBarSize = self.navigationController?.navigationBar.frame.size {
// barView.frame.size = CGSize(width: navBarSize.width, height: navBarSize.height + statusBarHeight)
self.navigationController?.navigationBar.subviews.first?.insertSubview(barView, at: 0)
NSLayoutConstraint.activate([
barView.leadingAnchor.constraint(equalTo: (self.navigationController?.navigationBar.subviews.first!.leadingAnchor)!),
barView.trailingAnchor.constraint(equalTo: (self.navigationController?.navigationBar.subviews.first!.trailingAnchor)!),
barView.heightAnchor.constraint(equalToConstant: navBarSize.height + statusBarHeight )
])
}
}
See the difference in notch device this:
