Search This Blog

Saturday, November 11, 2017

iOS Swift. How to create background view for status bar

Here I show you how to create custom background view for status bar. For demo example let create App with Navigation Controller and custom background color of Navigation bar. Code for that provided below. We will put it inside didFinishLaunchingWithOptions method.

func application(_ application: UIApplication, 
    didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
    UINavigationBar.appearance().barTintColor = UIColor(red: CGFloat(68.0/255.0),
                                                        green: CGFloat(151.0/255.0),
                                                        blue: CGFloat(227.0/255.0),
                                                        alpha: 1.0)
    
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.makeKeyAndVisible()
    let vc = ViewController()
    vc.view.backgroundColor = UIColor.white
    vc.title = "Custom Status Bar"
    self.window?.rootViewController = UINavigationController(rootViewController: vc)
}

This is how it looks.


Now we have blue color of navigation bar. It will be good to change status bar text color to white. We can do it by changing Info.plist file and set property 
View controller-based status bar appearance to NO


Make status bar text color white we can with settings in App Targets.


Or programmatically in didFinishLaunchingWithOptions method.
UIApplication.shared.statusBarStyle = .lightContent

Now we have white color of status bar text.


Let's go further. Now we need custom background color for status bar. For this purpose let's create special view and add it as subview to our window. Also we must provide couple of constraints.
let statusBarBackgroundView = UIView()
statusBarBackgroundView.translatesAutoresizingMaskIntoConstraints = false
statusBarBackgroundView.backgroundColor = UIColor(red: CGFloat(54.0/255.0),
                                                      green: CGFloat(120.0/255.0),
                                                      blue: CGFloat(181.0/255.0),
                                                      alpha: 1.0)
self.window?.addSubview(statusBarBackgroundView)
self.window?.addConstraints(
            NSLayoutConstraint.constraints(withVisualFormat: "V:|[b(20)]",
                                       options: NSLayoutFormatOptions(),
                                       metrics: nil,
                                       views: ["b" : statusBarBackgroundView]))
self.window?.addConstraints(
            NSLayoutConstraint.constraints(withVisualFormat: "H:|[b]|",
                                       options: NSLayoutFormatOptions(),
                                       metrics: nil,
                                       views: ["b" : statusBarBackgroundView]))

Here is result.


Full Code
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, 
        didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        UINavigationBar.appearance().barTintColor = UIColor(red: CGFloat(68.0/255.0),
                                                            green: CGFloat(151.0/255.0),
                                                            blue: CGFloat(227.0/255.0),
                                                            alpha: 1.0)
        
        UIApplication.shared.statusBarStyle = .lightContent
    
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.makeKeyAndVisible()
        let vc = ViewController()
        vc.view.backgroundColor = UIColor.white
        vc.title = "Custom Status Bar"
        self.window?.rootViewController = UINavigationController(rootViewController: vc)
        
        let statusBarBackgroundView = UIView()
        statusBarBackgroundView.translatesAutoresizingMaskIntoConstraints = false
        statusBarBackgroundView.backgroundColor = UIColor(red: CGFloat(54.0/255.0),
                                                          green: CGFloat(120.0/255.0),
                                                          blue: CGFloat(181.0/255.0),
                                                          alpha: 1.0)
        self.window?.addSubview(statusBarBackgroundView)
        self.window?.addConstraints(
            NSLayoutConstraint.constraints(withVisualFormat: "V:|[b(20)]",
                                           options: NSLayoutFormatOptions(),
                                           metrics: nil,
                                           views: ["b" : statusBarBackgroundView]))
        self.window?.addConstraints(
            NSLayoutConstraint.constraints(withVisualFormat: "H:|[b]|",
                                           options: NSLayoutFormatOptions(),
                                           metrics: nil,
                                           views: ["b" : statusBarBackgroundView]))
        
        return true
    }
}

5 comments:

  1. There are many articles circulating on internet that exaggerate about Custom Designed Websites. But your article is an exception that made me understand it without any difficulty.

    ReplyDelete
  2. Thank you for sharing this important and useful information in this article. Best Custom Websites

    ReplyDelete
  3. Your article is unbelievable with precise knowledge. I can see a lot of research behind that and that is beyond my expectations.
    Create Your Own Website \

    ReplyDelete
  4. This is a great article! Thank you very much. I really need these suggestions. An in-depth explanation is of great benefit. Incredibly inspiring to see how you write. Custom Website

    ReplyDelete
  5. This is a fantastic article. Your essay is quite simple to comprehend. Thank you for sharing this informative information with us.
    Search Engine Marketing Agency

    ReplyDelete