Reputation: 1754
Now I'm trying to make my app compatible for iPhone 6/6+. The issue is, I have 4 buttons horizontally aligned in one row. The width of each of them is 80 points(set in the storyboard). It's fine for iPhone4/5/5s. Now when I simulated the app on iPhone 6/6+, you can see there's 2 pixels space between the 3rd and 4th buttons. I'm not using auto layout. I know the width of iPhone 6 plus is: 1242 x 2208px. The problem is 1242 cannot be divided by 4.
The issue is when I use following code:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
CGFloat screenWidth = screenSize.width;
Actually the screenWidth
is 640.
Anybody know how to sort out this problem? Many thanks in advance.
Upvotes: 0
Views: 650
Reputation: 13313
You should be working in points, not pixels, so the iPhone 6 Plus has a width (when in portrait mode) of 414, and the iPhone 6 is 375.
When dividing up the screen in this situation, i.e., where the math doesn't work out exactly, you'll want to ensure the smallest spaces are equal (so the dividers in this case), and make the large areas slightly different sizes (off by a point etc). This is because (usually) the eye will be able to notice the difference between 1 and 2 points, but not a difference between say 102 and 103 points.
You may want to look at setting up this view with Auto Layout though instead of doing the math yourself. This is what's it's made for.
Regarding your second point about the variable screenWidth
being 640
, make sure your app isn't in compatibility mode. If it is, the 6 and 6+ with both return dimensions as if they're a 5s, and that would explain the 640
number. Add a LaunchScreen.xib
to ensure your app runs at the true screen resolution instead of scaling. See here: How to enable native resolution for apps on iPhone 6 and 6 Plus?
Upvotes: 2