Swift: Determine iOS Screen size
In Swift 5.0
let screenSize: CGRect = UIScreen.main.bounds
Swift 4.0
// Screen width.
public var screenWidth: CGFloat {
return UIScreen.main.bounds.width
}
// Screen height.
public var screenHeight: CGFloat {
return UIScreen.main.bounds.height
}
In Swift 3.0
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
In older swift:
Do something like this:
let screenSize: CGRect = UIScreen.mainScreen().bounds
then you can access the width and height like this:
let screenWidth = screenSize.width
let screenHeight = screenSize.height
if you want 75% of your screen's width you can go:
let screenWidth = screenSize.width * 0.75
How to get window size in swift (including iPad split screen)
I've found the answer
let screenSize : CGRect = self.view.frame
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
In Swift, how to determine the physical size of a device screen?
You may want to consider working with the frame property of the view of the current view controller.
self.view.frame.size.height and self.view.frame.size.width
You can make your other views proportional to those dimensions or based on their values, etc.
how to get the screen size until the bottom of the tabbar
For button origin use safe are insets, ıf device has notch safeAreaBottom will be like 44 , if not it will be 0:
let safeAreaBottom = UIApplication.shared.windows.first?.safeAreaInsets.bottom
Then use it :
newButtonFrame.origin.y = view.bounds.height - safeAreaBottom - 65 - 10
Related Topics
Constant Movement in Spritekit
How to Find the Address of a Stack Trace in Lldb for iOS
How to Filter Firebase Data in Swift
React-Native Run-iOS Can Not Find Any Simulator
Changing Uitableview's Section Header/Footer Title Without Reloading the Whole Table View
How to Make Nsurlsession Post Request in Swift
The Sandbox Is Not in Sync with the Podfile.Lock-Ios
How to Send Email Using Mfmailcomposeviewcontroller in Simulator
How to Do Weak Linking in Swift
Main.Async VS Main.Sync() VS Global().Async in Swift3 Gcd
Find Uialertview Without Having Reference to It iOS 7
Get Instance of Viewcontroller from Appdelegate in Swift
How to Get Memory Usage in Swift
What's the Point of Nsassert, Actually
Overlay an Image Over Another Image in iOS
Resize Superview After Subviews Change Dynamically Using Autolayout