Want to get the screen width and height 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:
1 2 3 | CGRect screenRect = [[UIScreen mainScreen] bounds]; CGFloat screenWidth = screenRect.size.width; CGFloat screenHeight = screenRect.size.height; |
1 2 3 4 5 6 7 8 | 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 |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.