Add a Uiview Above All, Even the Navigation Bar

Add UIView above NavigationBar

you forgot to set the frame of view and label, do like

let newView = UIView()
newView.frame = CGRect(x:0,y:UIApplication.shared.statusBarFrame.height, width: view.frame.size.width, height: self.navigationController!.navigationBar.frame.height)
newView.backgroundColor = UIColor.red
let label = UILabel()
label.frame = newView.bounds
label.text = "header"
newView.addSubview(label)
UIApplication.shared.keyWindow?.addSubview(newView)

you get OP as

Sample Image

Option 2

as per your required

    override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Do any additional setup after loading the view, typically from a nib.
let newView = UIView()
newView.frame = CGRect(x:0,y:UIApplication.shared.statusBarFrame.height, width: view.frame.size.width, height: 30)
newView.backgroundColor = UIColor.red
let label = UILabel()
label.frame = newView.bounds
label.text = "header"
newView.addSubview(label)
UIApplication.shared.keyWindow?.addSubview(newView)

let bounds = self.navigationController!.navigationBar.bounds
self.navigationController?.navigationBar.frame = CGRect(x: 0, y: newView.frame.size.height + newView.frame.origin.y, width: bounds.width, height: bounds.height )
}

Sample Image

UIView above NavigationBar and StatusBar

The below code will help you with that. You can change the color to your desired color to achieve the desired affect.

UIWindow *window = [[[UIApplication sharedApplication] delegate] window];

UIView *blue = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[blue setBackgroundColor:[UIColor blueColor]];
[window addSubview: blueView];

Swift Code:

if let applicationDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate? {
if let window:UIWindow = applicationDelegate.window {
let blueView:UIView = UIView(frame: UIScreen.main.bounds)
blueView.backgroundColor = UIColor.blue.withAlphaComponent(0.75)
window.addSubview(blueView)
}
}

Show UiView above all views

Add overlayView on main window:

if let window = UIApplication.shared.keyWindow {
let overlayView = UIView()
overlayView.frame = window.frame
overlayView.backgroundColor = .red
window.addSubview(overlayView)
}

How to add view on top of navigation bar


Swift 4.2, Xcode 10+

Okay, from what I can tell (via your comment reply, though it still isn't 100% clear), the best solution to your question would be to make the navigation bar transparent, such that you can see any navigationController-presented view controllers underneath it. For this, I'd suggest the following extension to UIViewController:

extension UIViewController {    
func setupTransparentNavigationBarWithBlackText() {
setupTransparentNavigationBar()
//Status bar text and back(item) tint to black
self.navigationController?.navigationBar.barStyle = .default
self.navigationController?.navigationBar.tintColor = .black
}

func setupTransparentNavigationBarWithWhiteText() {
setupTransparentNavigationBar()
//Status bar text and back(item) tint to white
self.navigationController?.navigationBar.barStyle = .blackTranslucent
self.navigationController?.navigationBar.tintColor = .white
}

func setupTransparentNavigationBar() {
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.backgroundColor = .clear
self.navigationController?.navigationBar.isTranslucent = true
}
}

Using either of the first two methods in viewWillAppear of your UIViewController subclasses will let you make the navigation bar completely transparent with the statusBar text + wifi/battery indicators black or white as desired. From this, you can then display anything under the navigation bar by pinning your constraints to view.bounds.topAnchor. E.g. for a transparent navigation controller with white statusBar text:

class YourViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
setupTransparentNavigationBarWithWhiteText()
}
}


Related Topics



Leave a reply



Submit