Get safe area inset top and bottom heights
On the new iPhone X, what would be the most proper way to get both top and bottom height for the unsafe areas?
ios iphone-x safearealayoutguide
add a comment |
On the new iPhone X, what would be the most proper way to get both top and bottom height for the unsafe areas?
ios iphone-x safearealayoutguide
add a comment |
On the new iPhone X, what would be the most proper way to get both top and bottom height for the unsafe areas?
ios iphone-x safearealayoutguide
On the new iPhone X, what would be the most proper way to get both top and bottom height for the unsafe areas?
ios iphone-x safearealayoutguide
ios iphone-x safearealayoutguide
edited Nov 7 '17 at 18:29
Tulleb
asked Oct 19 '17 at 12:25
TullebTulleb
5,79941840
5,79941840
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
Try this :
In Objective C
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat topPadding = window.safeAreaInsets.top;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
}
In Swift
if #available(iOS 11.0, *) {
let window = UIApplication.shared.keyWindow
let topPadding = window?.safeAreaInsets.top
let bottomPadding = window?.safeAreaInsets.bottom
}
1
@KrutikaSonawala bottomPadding + 10( your value)
– user6788419
Apr 23 '18 at 6:13
4
This doesn't seem to work for me - printing out the value of UIApplication.shared.keyWindow?.safeAreaInsets returns all 0s. Any ideas?
– Hai
Jun 20 '18 at 20:39
2
I did, I can constrain against safeAreaLayoutGuides of any view but directly printing out the value of the key window's safeAreaInsets just returns a zero rect. This is problematic because I need to manually constrain a view that has been added to the key window but is not in the root view controller's view hierarhcy.
– Hai
Jun 23 '18 at 14:27
1
doesn't work for me when use iphone x
– Bent El-Eslam
Jul 15 '18 at 6:44
1
For me,keyWindow
was alwaysnil
so I changed it towindows[0]
and removed the?
from the optional chaining, then it worked.
– George_E
Nov 3 '18 at 17:02
|
show 13 more comments
To get the height between the layout guides you just do
let guide = view.safeAreaLayoutGuide
let height = guide.layoutFrame.size.height
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 updated the answer
– Ladislav
Oct 19 '17 at 14:41
Thanks that was the lead I was also thinking about. I don't think there is a more reliable solution. But with this I only get the full unsafe height, right? Not two height for each areas?
– Tulleb
Oct 19 '17 at 14:45
1
Once views have been laid out this will give you the correct answer
– Ladislav
Oct 19 '17 at 14:47
2
Actually @user6788419's answer looks nice too and way shorter.
– Tulleb
Oct 19 '17 at 14:50
1
@FerranMaylinch Not sure where you are checking the height, but I also had 812, which turned out to be due to checking the value before the view was visible. In that case the heights will be the same "If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the layout guide edges are equal to the edges of the view" developer.apple.com/documentation/uikit/uiview/…
– Nicholas Harlen
Jul 10 '18 at 3:58
|
show 1 more comment
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:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var topSafeArea: CGFloat
var bottomSafeArea: CGFloat
if #available(iOS 11.0, *) {
topSafeArea = view.safeAreaInsets.top
bottomSafeArea = view.safeAreaInsets.bottom
} else {
topSafeArea = topLayoutGuide.length
bottomSafeArea = bottomLayoutGuide.length
}
// safe area values are now available to use
}
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.
3
Please declare using let topSafeArea: CGFloat instead!
– Gusutafu
Apr 16 '18 at 17:38
add a comment |
I'm working with CocoaPods frameworks and in case UIApplication.shared
is unavailable then I use safeAreaInsets
in view's window
:
if #available(iOS 11.0, *) {
let insets = view.window?.safeAreaInsets
let top = insets.top
let bottom = insets.bottom
}
add a comment |
None of the other answers here worked for me, but this did.
var topSafeAreaHeight: CGFloat = 0
var bottomSafeAreaHeight: CGFloat = 0
if #available(iOS 11.0, *) {
let window = UIApplication.shared.windows[0]
let safeFrame = window.safeAreaLayoutGuide.layoutFrame
topSafeAreaHeight = safeFrame.minY
bottomSafeAreaHeight = window.frame.maxY - safeFrame.maxY
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46829840%2fget-safe-area-inset-top-and-bottom-heights%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this :
In Objective C
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat topPadding = window.safeAreaInsets.top;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
}
In Swift
if #available(iOS 11.0, *) {
let window = UIApplication.shared.keyWindow
let topPadding = window?.safeAreaInsets.top
let bottomPadding = window?.safeAreaInsets.bottom
}
1
@KrutikaSonawala bottomPadding + 10( your value)
– user6788419
Apr 23 '18 at 6:13
4
This doesn't seem to work for me - printing out the value of UIApplication.shared.keyWindow?.safeAreaInsets returns all 0s. Any ideas?
– Hai
Jun 20 '18 at 20:39
2
I did, I can constrain against safeAreaLayoutGuides of any view but directly printing out the value of the key window's safeAreaInsets just returns a zero rect. This is problematic because I need to manually constrain a view that has been added to the key window but is not in the root view controller's view hierarhcy.
– Hai
Jun 23 '18 at 14:27
1
doesn't work for me when use iphone x
– Bent El-Eslam
Jul 15 '18 at 6:44
1
For me,keyWindow
was alwaysnil
so I changed it towindows[0]
and removed the?
from the optional chaining, then it worked.
– George_E
Nov 3 '18 at 17:02
|
show 13 more comments
Try this :
In Objective C
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat topPadding = window.safeAreaInsets.top;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
}
In Swift
if #available(iOS 11.0, *) {
let window = UIApplication.shared.keyWindow
let topPadding = window?.safeAreaInsets.top
let bottomPadding = window?.safeAreaInsets.bottom
}
1
@KrutikaSonawala bottomPadding + 10( your value)
– user6788419
Apr 23 '18 at 6:13
4
This doesn't seem to work for me - printing out the value of UIApplication.shared.keyWindow?.safeAreaInsets returns all 0s. Any ideas?
– Hai
Jun 20 '18 at 20:39
2
I did, I can constrain against safeAreaLayoutGuides of any view but directly printing out the value of the key window's safeAreaInsets just returns a zero rect. This is problematic because I need to manually constrain a view that has been added to the key window but is not in the root view controller's view hierarhcy.
– Hai
Jun 23 '18 at 14:27
1
doesn't work for me when use iphone x
– Bent El-Eslam
Jul 15 '18 at 6:44
1
For me,keyWindow
was alwaysnil
so I changed it towindows[0]
and removed the?
from the optional chaining, then it worked.
– George_E
Nov 3 '18 at 17:02
|
show 13 more comments
Try this :
In Objective C
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat topPadding = window.safeAreaInsets.top;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
}
In Swift
if #available(iOS 11.0, *) {
let window = UIApplication.shared.keyWindow
let topPadding = window?.safeAreaInsets.top
let bottomPadding = window?.safeAreaInsets.bottom
}
Try this :
In Objective C
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat topPadding = window.safeAreaInsets.top;
CGFloat bottomPadding = window.safeAreaInsets.bottom;
}
In Swift
if #available(iOS 11.0, *) {
let window = UIApplication.shared.keyWindow
let topPadding = window?.safeAreaInsets.top
let bottomPadding = window?.safeAreaInsets.bottom
}
edited Feb 13 '18 at 6:35
Fahim Parkar
20.5k34131239
20.5k34131239
answered Oct 19 '17 at 13:49
user6788419user6788419
3,1131622
3,1131622
1
@KrutikaSonawala bottomPadding + 10( your value)
– user6788419
Apr 23 '18 at 6:13
4
This doesn't seem to work for me - printing out the value of UIApplication.shared.keyWindow?.safeAreaInsets returns all 0s. Any ideas?
– Hai
Jun 20 '18 at 20:39
2
I did, I can constrain against safeAreaLayoutGuides of any view but directly printing out the value of the key window's safeAreaInsets just returns a zero rect. This is problematic because I need to manually constrain a view that has been added to the key window but is not in the root view controller's view hierarhcy.
– Hai
Jun 23 '18 at 14:27
1
doesn't work for me when use iphone x
– Bent El-Eslam
Jul 15 '18 at 6:44
1
For me,keyWindow
was alwaysnil
so I changed it towindows[0]
and removed the?
from the optional chaining, then it worked.
– George_E
Nov 3 '18 at 17:02
|
show 13 more comments
1
@KrutikaSonawala bottomPadding + 10( your value)
– user6788419
Apr 23 '18 at 6:13
4
This doesn't seem to work for me - printing out the value of UIApplication.shared.keyWindow?.safeAreaInsets returns all 0s. Any ideas?
– Hai
Jun 20 '18 at 20:39
2
I did, I can constrain against safeAreaLayoutGuides of any view but directly printing out the value of the key window's safeAreaInsets just returns a zero rect. This is problematic because I need to manually constrain a view that has been added to the key window but is not in the root view controller's view hierarhcy.
– Hai
Jun 23 '18 at 14:27
1
doesn't work for me when use iphone x
– Bent El-Eslam
Jul 15 '18 at 6:44
1
For me,keyWindow
was alwaysnil
so I changed it towindows[0]
and removed the?
from the optional chaining, then it worked.
– George_E
Nov 3 '18 at 17:02
1
1
@KrutikaSonawala bottomPadding + 10( your value)
– user6788419
Apr 23 '18 at 6:13
@KrutikaSonawala bottomPadding + 10( your value)
– user6788419
Apr 23 '18 at 6:13
4
4
This doesn't seem to work for me - printing out the value of UIApplication.shared.keyWindow?.safeAreaInsets returns all 0s. Any ideas?
– Hai
Jun 20 '18 at 20:39
This doesn't seem to work for me - printing out the value of UIApplication.shared.keyWindow?.safeAreaInsets returns all 0s. Any ideas?
– Hai
Jun 20 '18 at 20:39
2
2
I did, I can constrain against safeAreaLayoutGuides of any view but directly printing out the value of the key window's safeAreaInsets just returns a zero rect. This is problematic because I need to manually constrain a view that has been added to the key window but is not in the root view controller's view hierarhcy.
– Hai
Jun 23 '18 at 14:27
I did, I can constrain against safeAreaLayoutGuides of any view but directly printing out the value of the key window's safeAreaInsets just returns a zero rect. This is problematic because I need to manually constrain a view that has been added to the key window but is not in the root view controller's view hierarhcy.
– Hai
Jun 23 '18 at 14:27
1
1
doesn't work for me when use iphone x
– Bent El-Eslam
Jul 15 '18 at 6:44
doesn't work for me when use iphone x
– Bent El-Eslam
Jul 15 '18 at 6:44
1
1
For me,
keyWindow
was always nil
so I changed it to windows[0]
and removed the ?
from the optional chaining, then it worked.– George_E
Nov 3 '18 at 17:02
For me,
keyWindow
was always nil
so I changed it to windows[0]
and removed the ?
from the optional chaining, then it worked.– George_E
Nov 3 '18 at 17:02
|
show 13 more comments
To get the height between the layout guides you just do
let guide = view.safeAreaLayoutGuide
let height = guide.layoutFrame.size.height
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 updated the answer
– Ladislav
Oct 19 '17 at 14:41
Thanks that was the lead I was also thinking about. I don't think there is a more reliable solution. But with this I only get the full unsafe height, right? Not two height for each areas?
– Tulleb
Oct 19 '17 at 14:45
1
Once views have been laid out this will give you the correct answer
– Ladislav
Oct 19 '17 at 14:47
2
Actually @user6788419's answer looks nice too and way shorter.
– Tulleb
Oct 19 '17 at 14:50
1
@FerranMaylinch Not sure where you are checking the height, but I also had 812, which turned out to be due to checking the value before the view was visible. In that case the heights will be the same "If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the layout guide edges are equal to the edges of the view" developer.apple.com/documentation/uikit/uiview/…
– Nicholas Harlen
Jul 10 '18 at 3:58
|
show 1 more comment
To get the height between the layout guides you just do
let guide = view.safeAreaLayoutGuide
let height = guide.layoutFrame.size.height
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 updated the answer
– Ladislav
Oct 19 '17 at 14:41
Thanks that was the lead I was also thinking about. I don't think there is a more reliable solution. But with this I only get the full unsafe height, right? Not two height for each areas?
– Tulleb
Oct 19 '17 at 14:45
1
Once views have been laid out this will give you the correct answer
– Ladislav
Oct 19 '17 at 14:47
2
Actually @user6788419's answer looks nice too and way shorter.
– Tulleb
Oct 19 '17 at 14:50
1
@FerranMaylinch Not sure where you are checking the height, but I also had 812, which turned out to be due to checking the value before the view was visible. In that case the heights will be the same "If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the layout guide edges are equal to the edges of the view" developer.apple.com/documentation/uikit/uiview/…
– Nicholas Harlen
Jul 10 '18 at 3:58
|
show 1 more comment
To get the height between the layout guides you just do
let guide = view.safeAreaLayoutGuide
let height = guide.layoutFrame.size.height
So full frame height = 812.0
, safe area height = 734.0
Below is the example where the green view has frame of guide.layoutFrame
To get the height between the layout guides you just do
let guide = view.safeAreaLayoutGuide
let height = guide.layoutFrame.size.height
So full frame height = 812.0
, safe area height = 734.0
Below is the example where the green view has frame of guide.layoutFrame
edited Oct 19 '17 at 14:39
answered Oct 19 '17 at 13:45
LadislavLadislav
5,41132127
5,41132127
I updated the answer
– Ladislav
Oct 19 '17 at 14:41
Thanks that was the lead I was also thinking about. I don't think there is a more reliable solution. But with this I only get the full unsafe height, right? Not two height for each areas?
– Tulleb
Oct 19 '17 at 14:45
1
Once views have been laid out this will give you the correct answer
– Ladislav
Oct 19 '17 at 14:47
2
Actually @user6788419's answer looks nice too and way shorter.
– Tulleb
Oct 19 '17 at 14:50
1
@FerranMaylinch Not sure where you are checking the height, but I also had 812, which turned out to be due to checking the value before the view was visible. In that case the heights will be the same "If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the layout guide edges are equal to the edges of the view" developer.apple.com/documentation/uikit/uiview/…
– Nicholas Harlen
Jul 10 '18 at 3:58
|
show 1 more comment
I updated the answer
– Ladislav
Oct 19 '17 at 14:41
Thanks that was the lead I was also thinking about. I don't think there is a more reliable solution. But with this I only get the full unsafe height, right? Not two height for each areas?
– Tulleb
Oct 19 '17 at 14:45
1
Once views have been laid out this will give you the correct answer
– Ladislav
Oct 19 '17 at 14:47
2
Actually @user6788419's answer looks nice too and way shorter.
– Tulleb
Oct 19 '17 at 14:50
1
@FerranMaylinch Not sure where you are checking the height, but I also had 812, which turned out to be due to checking the value before the view was visible. In that case the heights will be the same "If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the layout guide edges are equal to the edges of the view" developer.apple.com/documentation/uikit/uiview/…
– Nicholas Harlen
Jul 10 '18 at 3:58
I updated the answer
– Ladislav
Oct 19 '17 at 14:41
I updated the answer
– Ladislav
Oct 19 '17 at 14:41
Thanks that was the lead I was also thinking about. I don't think there is a more reliable solution. But with this I only get the full unsafe height, right? Not two height for each areas?
– Tulleb
Oct 19 '17 at 14:45
Thanks that was the lead I was also thinking about. I don't think there is a more reliable solution. But with this I only get the full unsafe height, right? Not two height for each areas?
– Tulleb
Oct 19 '17 at 14:45
1
1
Once views have been laid out this will give you the correct answer
– Ladislav
Oct 19 '17 at 14:47
Once views have been laid out this will give you the correct answer
– Ladislav
Oct 19 '17 at 14:47
2
2
Actually @user6788419's answer looks nice too and way shorter.
– Tulleb
Oct 19 '17 at 14:50
Actually @user6788419's answer looks nice too and way shorter.
– Tulleb
Oct 19 '17 at 14:50
1
1
@FerranMaylinch Not sure where you are checking the height, but I also had 812, which turned out to be due to checking the value before the view was visible. In that case the heights will be the same "If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the layout guide edges are equal to the edges of the view" developer.apple.com/documentation/uikit/uiview/…
– Nicholas Harlen
Jul 10 '18 at 3:58
@FerranMaylinch Not sure where you are checking the height, but I also had 812, which turned out to be due to checking the value before the view was visible. In that case the heights will be the same "If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the layout guide edges are equal to the edges of the view" developer.apple.com/documentation/uikit/uiview/…
– Nicholas Harlen
Jul 10 '18 at 3:58
|
show 1 more comment
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:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var topSafeArea: CGFloat
var bottomSafeArea: CGFloat
if #available(iOS 11.0, *) {
topSafeArea = view.safeAreaInsets.top
bottomSafeArea = view.safeAreaInsets.bottom
} else {
topSafeArea = topLayoutGuide.length
bottomSafeArea = bottomLayoutGuide.length
}
// safe area values are now available to use
}
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.
3
Please declare using let topSafeArea: CGFloat instead!
– Gusutafu
Apr 16 '18 at 17:38
add a comment |
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:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var topSafeArea: CGFloat
var bottomSafeArea: CGFloat
if #available(iOS 11.0, *) {
topSafeArea = view.safeAreaInsets.top
bottomSafeArea = view.safeAreaInsets.bottom
} else {
topSafeArea = topLayoutGuide.length
bottomSafeArea = bottomLayoutGuide.length
}
// safe area values are now available to use
}
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.
3
Please declare using let topSafeArea: CGFloat instead!
– Gusutafu
Apr 16 '18 at 17:38
add a comment |
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:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var topSafeArea: CGFloat
var bottomSafeArea: CGFloat
if #available(iOS 11.0, *) {
topSafeArea = view.safeAreaInsets.top
bottomSafeArea = view.safeAreaInsets.bottom
} else {
topSafeArea = topLayoutGuide.length
bottomSafeArea = bottomLayoutGuide.length
}
// safe area values are now available to use
}
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.
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:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var topSafeArea: CGFloat
var bottomSafeArea: CGFloat
if #available(iOS 11.0, *) {
topSafeArea = view.safeAreaInsets.top
bottomSafeArea = view.safeAreaInsets.bottom
} else {
topSafeArea = topLayoutGuide.length
bottomSafeArea = bottomLayoutGuide.length
}
// safe area values are now available to use
}
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.
edited Jan 7 at 1:37
answered Apr 6 '18 at 1:23
bsodbsod
2,3061922
2,3061922
3
Please declare using let topSafeArea: CGFloat instead!
– Gusutafu
Apr 16 '18 at 17:38
add a comment |
3
Please declare using let topSafeArea: CGFloat instead!
– Gusutafu
Apr 16 '18 at 17:38
3
3
Please declare using let topSafeArea: CGFloat instead!
– Gusutafu
Apr 16 '18 at 17:38
Please declare using let topSafeArea: CGFloat instead!
– Gusutafu
Apr 16 '18 at 17:38
add a comment |
I'm working with CocoaPods frameworks and in case UIApplication.shared
is unavailable then I use safeAreaInsets
in view's window
:
if #available(iOS 11.0, *) {
let insets = view.window?.safeAreaInsets
let top = insets.top
let bottom = insets.bottom
}
add a comment |
I'm working with CocoaPods frameworks and in case UIApplication.shared
is unavailable then I use safeAreaInsets
in view's window
:
if #available(iOS 11.0, *) {
let insets = view.window?.safeAreaInsets
let top = insets.top
let bottom = insets.bottom
}
add a comment |
I'm working with CocoaPods frameworks and in case UIApplication.shared
is unavailable then I use safeAreaInsets
in view's window
:
if #available(iOS 11.0, *) {
let insets = view.window?.safeAreaInsets
let top = insets.top
let bottom = insets.bottom
}
I'm working with CocoaPods frameworks and in case UIApplication.shared
is unavailable then I use safeAreaInsets
in view's window
:
if #available(iOS 11.0, *) {
let insets = view.window?.safeAreaInsets
let top = insets.top
let bottom = insets.bottom
}
answered Nov 25 '18 at 4:02
Tai LeTai Le
2,86612320
2,86612320
add a comment |
add a comment |
None of the other answers here worked for me, but this did.
var topSafeAreaHeight: CGFloat = 0
var bottomSafeAreaHeight: CGFloat = 0
if #available(iOS 11.0, *) {
let window = UIApplication.shared.windows[0]
let safeFrame = window.safeAreaLayoutGuide.layoutFrame
topSafeAreaHeight = safeFrame.minY
bottomSafeAreaHeight = window.frame.maxY - safeFrame.maxY
}
add a comment |
None of the other answers here worked for me, but this did.
var topSafeAreaHeight: CGFloat = 0
var bottomSafeAreaHeight: CGFloat = 0
if #available(iOS 11.0, *) {
let window = UIApplication.shared.windows[0]
let safeFrame = window.safeAreaLayoutGuide.layoutFrame
topSafeAreaHeight = safeFrame.minY
bottomSafeAreaHeight = window.frame.maxY - safeFrame.maxY
}
add a comment |
None of the other answers here worked for me, but this did.
var topSafeAreaHeight: CGFloat = 0
var bottomSafeAreaHeight: CGFloat = 0
if #available(iOS 11.0, *) {
let window = UIApplication.shared.windows[0]
let safeFrame = window.safeAreaLayoutGuide.layoutFrame
topSafeAreaHeight = safeFrame.minY
bottomSafeAreaHeight = window.frame.maxY - safeFrame.maxY
}
None of the other answers here worked for me, but this did.
var topSafeAreaHeight: CGFloat = 0
var bottomSafeAreaHeight: CGFloat = 0
if #available(iOS 11.0, *) {
let window = UIApplication.shared.windows[0]
let safeFrame = window.safeAreaLayoutGuide.layoutFrame
topSafeAreaHeight = safeFrame.minY
bottomSafeAreaHeight = window.frame.maxY - safeFrame.maxY
}
answered Dec 20 '18 at 7:17
ScottyBladesScottyBlades
1,6691525
1,6691525
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46829840%2fget-safe-area-inset-top-and-bottom-heights%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown