0

Why cornerRadius and maskToBounds properties won't affect statusBar if I set background color for statusBar?

(I'm a beginner in swift)

CONTEXT

What I want to achieve:

I want to set cornerRadius for every corner of the screen within the app.


What have I done so far?:

// I added the following in appDelegate:didFinishLaunchingWithOptions
self.window?.layer.cornerRadius = 20
self.window?.layer.masksToBounds = true

It works fine in every corner of the screen IF statusBar doesn't have background color.

If backgroundColor is set for statusBar, only the bottom corners of the screen will be rounded.

What I suspect is failing: I suspect the statusBar is within a layer on top of the layer that holds the window in which I'm setting the cornerRadius property and that's why it doesn't render with that property. If statusBar has no backgroundColor (.clear) it doesn't cover the window, so I can see the 4 corners rounded in the screen.

That's why I tried to set statusBar's cornerRadius property:

// Still in appDelegate:didFinishLaunchingWithOptions
UIApplication.shared.statusBarView?.layer.cornerRadius = 20
UIApplication.shared.statusBarView?.layer.masksToBounds = true
UIApplication.shared.statusBarView?.backgroundColor = .blue

self.window?.layer.cornerRadius = 20
self.window?.layer.masksToBounds = true

It does round the corners in statusBar if its value is lower than its height, BUT if cornerRadius value is higher than statusBar's height it starts to behave weird, leaving some black space between itself and the navigationBar.


Is there a way to set a "global mask" or "global property" that also affects statusBar?

I thought that by only setting cornerRadius for top corners would work, but, again, if cornerRadius is higher that its height, it renders leaving some unwanted blank space.

Suggestions to achieve what I want in other ways are welcome

Thank you :)

Ryoko
  • 75
  • 6
  • Your issue is related to the safe area of the screen versus the window. I would recommend manipulating the view contained within the safe area if you don't want it to be affected by changes to the status bar. The window portion technically includes the background under the status bar, so any changes to the status bar would mask the window. – binaryPilot84 Sep 05 '18 at 15:56
  • Thank you @John Ayers , I tried with this but still does not work: `UIApplication.shared.keyWindow?.layer.cornerRadius = 20 UIApplication.shared.keyWindow?.layer.masksToBounds = true UIApplication.shared.keyWindow?.layer.isOpaque = true UIApplication.shared.keyWindow?.clipsToBounds = true` – Ryoko Sep 05 '18 at 16:26

0 Answers0