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
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 )
}
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
Firebase with Swift 3 Counting the Number of Children
What's the Uiscrollview Contentinset Property For
Usage of Protocols as Array Types and Function Parameters in Swift
Error When Trying to Obtain a Certificate: the Specified Item Could Not Be Found in the Keychain
Uiapplication.Registerforremotenotifications() Must Be Called from Main Thread Only
iOS Enterprise Ota Distribution Unable to Download Application
How Much Delay of iOS Push Notification
How to Add Image in Uitableviewrowaction
Amazon Aws iOS Sdk: How to List All File Names in a Folder
How to Debug "Terminated Due to Memory Error"
Xcode - Bundle Format Unrecognized, Invalid, or Unsuitable
How to Check If a File Exists in Documents Folder
iPhone Ad Hoc Build Using Xcode 4
Refresh Devices in Team Provisioning Profile Managed by Xcode 7
Xcode 10: Unable to Attach Db Error