Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Michael Zanfardino
Michael Zanfardino
2,416 Points

UIView height for iPhone X vs other iPhones

How do you have a fixed height for a UIView for the iPhone X vs other iPhones? I would like for the height of the UIView when viewed on an iPhone X be 35pt and for all other iPhones (iPads) be 22pt.

Can this be achieved via the storyboard?

Thank you in advance.

mz

2 Answers

Michael Hulet
Michael Hulet
47,913 Points

You really shouldn't do this. It violates Apple's Human Interface Guidelines to design for specific devices like that, and it's ultimately really hard/hacky to implement properly. However, if you're intent on it, you can detect it by the fact that the iPhone X has a unique screen size right now. Thus, you can inspect the UIScreen and compare to see if it matches the iPhone X's resolution, like this:

Swift

let iPhoneXScreenHeight = 2436 // This is in pixels

if case UIScreen.main.nativeBounds = iPhoneXScreenHeight{
    // Do your iPhone X-specific stuff here
}

Objective-C

const CGFloat iPhoneXScreenHeight = 2436; // This is in pixels

if(UIScreen.mainScreen.nativeBounds == iPhoneXScreenHeight){
    // Do your iPhone X-specific stuff here
}
Michael Zanfardino
Michael Zanfardino
2,416 Points

Hi Michael,

Thank you for the quick response. Yes, I completely understand the concern with Apples Human Interface Guidelines.

Here is a little more info for my current situation. We have a background image that is displayed on the home screen of our app and is made up of light colors (mostly white). So, this makes the status bar at the top of the phone barely visible for users. Before iPhone X, I would just add a custom view to the homescreen.storyboard that had a transparency of .15 and a height of 22. But, when viewed on the iPhone X the UIView needs a larger height...about 35.

I basically did what bullet 2 recommends here (https://developer.apple.com/ios/human-interface-guidelines/bars/status-bars/). So, is there a way to change the height for iPhone X only or do you have another suggestion?

Appreciate your time and help.

mz