How to set safe area background color in swift 4.2?
Solution 1
just set the color to your view: view.backgroundColor = .blue
and set constraints to your view, that it is as big as the safe area.
Solution 2
I was struggling with this one for an hour so I thought I'd share my solution if anyone wants it.
This DID NOT work for me👇
extension UIApplication
{
var statusBarView: UIView?
{
return value(forKey: "statusBar") as? UIView
}
}
// Calling this 👇 crashed
// UIApplication.shared.statusBarView?.backgroundColor = UIColor.black
So I just made a different extension👇
extension UIViewController
{
func topColouredBlack()
{
let colouredTopBlack = UIView()
view.addSubview(colouredTopBlack)
colouredTopBlack.translatesAutoresizingMaskIntoConstraints = false
colouredTopBlack.backgroundColor = .black
NSLayoutConstraint.activate([
colouredTopBlack.topAnchor.constraint(equalTo: view.topAnchor),
colouredTopBlack.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
colouredTopBlack.widthAnchor.constraint(equalTo: view.widthAnchor),
])
}
}
I just call topColouredBlack() wherever I want & I made another one of a different colour when I want to change it back.
func scrollViewWillBeginDragging(_ scrollView: UIScrollView)
{
topColouredBlack()
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
// topColouredWhite()
}
Solution 3
have you tried this code?
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
}
}
Related videos on Youtube
saleumsack
I'm software developer. live in Vientiane Capital, Laos P.D.R
Updated on June 04, 2022Comments
-
saleumsack almost 2 years
I'm a very beginner in swift
I have problem with the safe area view.
For the top(some said "Status bar") I've done with this code for changing the background color
if #available(iOS 13.0, *) { let app = UIApplication.shared let statusBarHeight: CGFloat = app.statusBarFrame.size.height let statusbarView = UIView() statusbarView.backgroundColor = hexStringToUIColor(hex: "#7f0000") view.addSubview(statusbarView) statusbarView.translatesAutoresizingMaskIntoConstraints = false statusbarView.heightAnchor .constraint(equalToConstant: statusBarHeight).isActive = true statusbarView.widthAnchor .constraint(equalTo: view.widthAnchor, multiplier: 1.0).isActive = true statusbarView.topAnchor .constraint(equalTo: view.topAnchor).isActive = true statusbarView.centerXAnchor .constraint(equalTo: view.centerXAnchor).isActive = true } else { let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView statusBar?.backgroundColor = hexStringToUIColor(hex: "#7f0000") }
I have no idea how to change the bottom safe area view background color?
my Main.storyboard my storyboard
-
saleumsack over 4 yearsThank you so much @Chris this is works as I expect. You save my time! :)