Monday, November 25, 2019

Different Bounds between UIScreen.main.bounds and Self.view.bounds


As you know guys, sometimes we want to get height of the screen or the width of the screen. And It's different for every devices. But it's surprisingly different if you call it in ViewDidLayout and ViewDidApper.

Why it can be different ? because in ViewDidLayout the view is not loaded in device screen. And it's bad to get height or width from there. But if you call UIScreen.main.bounds, it will return the right size because UIScreen no need to wait in ViewDidAppear like self.view.bounds.

The user will see some things, it's not good. Like the height change but it's not animation but the bug of waiting the right width. And I encounter that problem, it needs a solution. But my solution before it's not good. Because I just show when the user have arrived and it looks like there's delay displaying the view.

So the solution is initialize the view in ViewDidLayout but using UIScreen.main.bounds to get the right bounds and not self.view.bounds. Self.view.bounds it's good to be used in ViewDidAppear when screen is opened by user.

So this is the code guys, it's simple but really helpful :D.



So it's very important to know the different. Soon or later you will face this problem. Height and Width different and you don't know why. Everything seem looks good but remember this article, you need to concern about bounds.

If you have any question please feel free to ask. I will help u as much as I can. Thanks for visiting my simple blog.

No comments:

Post a Comment