Why Are Uiscreen.Bounds Incorrect in iOS11

Swift - Why is UIScreen.main.bounds behaving differently on different devices?

You will face many problems should you pursue in this direction!

You should not use the screen size but your view controller's view bounds.
Doing so will avoid size problems, should your app be used in multitask or otherwise...

override func viewDidLoad() {
super.viewDidLoad()
let skView = SKView(frame: self.view.bounds)
view.addSubview(skView);
let scene = MyScene(size: view.bounds.size) // <- construct your scene with the screen size
skView.presentScene(scene)
}

You can then fill your scene and use the size value to layout your nodes.

iPhone XR returns correct nativeBounds but has wrong screen size on device

In my case the trouble was in the specific user device settings.
Settings -> Display & brightness -> Display zoom switch to ON.
This cause to return wrong

[UIScreen mainScreen].bounds.size

values runtime

How to get the screen width and height in iOS?

How can one get the dimensions of the screen in iOS?

The problem with the code that you posted is that you're counting on the view size to match that of the screen, and as you've seen that's not always the case. If you need the screen size, you should look at the object that represents the screen itself, like this:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

Update for split view: In comments, Dmitry asked:

How can I get the size of the screen in the split view?

The code given above reports the size of the screen, even in split screen mode. When you use split screen mode, your app's window changes. If the code above doesn't give you the information you expect, then like the OP, you're looking at the wrong object. In this case, though, you should look at the window instead of the screen, like this:

CGRect windowRect = self.view.window.frame;
CGFloat windowWidth = windowRect.size.width;
CGFloat windowHeight = windowRect.size.height;

Swift 4.2

let screenRect = UIScreen.main.bounds
let screenWidth = screenRect.size.width
let screenHeight = screenRect.size.height

// split screen
let windowRect = self.view.window?.frame
let windowWidth = windowRect?.size.width
let windowHeight = windowRect?.size.height

How to fix UILabel intrinsicContentSize on iOS 11

So through experimenting on the playground I was able to come up with a solution that involves testing for the extremely large intrinsic content size.

I noticed that all UILabels that misbehave have numberOfLines==0 and preferredMaxLayoutWidth=0. On subsequent layout passes, UIKit sets preferredMaxLayoutWidth to a non-zero value, presumably to iterate onto the correct height for the label. So the first fix was to try temporarily setting numberOfLines when (self.label.numberOfLines == 0 && self.label.preferredMaxLayoutWidth == 0).

I also noticed that all UILabels that have these two properties as 0 do not necessarily misbehave. (i.e. the inverse isn't true). So this fix worked, but modified the label unnecessarily some of the time. It also has a small bug that when the label's text contains \n newlines, number of lines should be set to the number of lines in the string, not 1.

The final solution I came to is a little more hacky, but specifically looks for UILabel misbehaving and only kick's it then...

override var intrinsicContentSize: CGSize {

guard super.intrinsicContentSize.width > 1000000000.0 else {

return super.intrinsicContentSize
}

var count = 0

if let text = self.text {

text.enumerateLines {(_, _) in
count += 1
}

} else {

count = 1
}

let oldNumberOfLines = self.numberOfLines

self.numberOfLines = count
let size = super.intrinsicContentSize

self.numberOfLines = oldNumberOfLines

return size
}

You can find this as a Gist here.

iOS 11 & iPhone X: UINavigationBar's toolbar spacing incorrect when embedded in UITabBarController

I filed this as radr://problem/34421298, which was closed as a duplicate of radr://problem/34462371. However, in the latest beta of Xcode 9.2 (9C32c) with iOS 11.2, this seems to be fixed. Here's an example of my app running in the simulator of each device, with no changes in between.

Navbar toolbar under iOS 11.1 and 11.2

This isn't really a solution to your problem, other than that some patience may solve it without needing to resort to UI trickery. My assumption is that iOS 11.2 will be out before the end of the year, since it's needed to support HomePod.



Related Topics



Leave a reply



Submit