How to set unsafe area background color for ios 11

后端 未结 3 1916
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 18:23

Creating some new view controllers with xcode 9 so now I have a few safe areas to deal with.

I am currently trying to do something fullproof, meaning keeping the uns

相关标签:
3条回答
  • 2020-12-28 18:43

    It looks like a hacky trick but you may try this:
    You can set background color for status bar during application launch or during viewDidLoad of your view controller. Here it works for me, in following ways.

    extension UIApplication {
    
        var statusBarView: UIView? {
            return value(forKey: "statusBar") as? UIView
        }
    
    }
    
    // Set it from your view controller if you've view controller based statusbar
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            UIApplication.shared.statusBarView?.backgroundColor = UIColor.green
        }
    
    }
    
    or
    
    // Set upon application launch, if you've application based status bar
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            UIApplication.shared.statusBarView?.backgroundColor = UIColor.green
            return true
        }
    }
    



    Here is result:

    enter image description here

    0 讨论(0)
  • 2020-12-28 18:50
        if #available(iOS 13.0, *) {
            let navBarAppearance = UINavigationBarAppearance()
            navBarAppearance.configureWithTransparentBackground()
            navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
            navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
            navBarAppearance.backgroundColor = .black
            navigationController?.navigationBar.standardAppearance = navBarAppearance
            navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
        }
    
    0 讨论(0)
  • 2020-12-28 18:55

    You have to apply different constraints. Your background color should extend beyond the safe area all the way to the superview. So your constraints need to be set to the superview for your background color but to the safe area for your ui view (buttons, tableViews and the like)

    0 讨论(0)
提交回复
热议问题