On the new iPhone X, what would be the most proper way to get both top and bottom height for the unsafe areas?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
None of the other answers here worked for me, but this did.
Swift 4
To pin a view to a safe area anchor using constraints can be done at any point in the view controller's lifecycle because constraints are handled by the auto layout API behind the scenes. However, to access the values of those safe areas to determine their heights requires waiting until later in the view controller's lifecycle, ideally
viewDidLayoutSubviews()
.This plugs into any view controller:
I prefer this method to getting it off of the window because it’s how Apple designed the API to be used. Don't reference extraneous objects like the window when everything is already in the view controller. And by using
viewDidLayoutSubviews()
, safe area values are updated during orientation changes, double-height status bars, and all other mutations to the view and its safe areas.Try this :
In Objective C
In Swift
To get the height between the layout guides you just do
So
full frame height = 812.0
,safe area height = 734.0
Below is the example where the green view has frame of
guide.layoutFrame
I'm working with CocoaPods frameworks and in case
UIApplication.shared
is unavailable then I usesafeAreaInsets
in view'swindow
: