Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 306 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Swift: Determine iOS Screen size

#1
I would like to use Swift code to properly position items in my app for no matter what the screen size is. For example, if I want a button to be 75% of the screen wide, I could do something like `(screenWidth * .75)` to be the width of the button. I have found that this could be determined in Objective-C by doing

CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;

Unfortunately, I am unsure of how to convert this to Swift. Does anyone have an idea?

Thanks!
Reply

#2

**In Swift 5.0**

let screenSize: CGRect = UIScreen.main.bounds


Pay attention that `UIScreen.main` will be deprecated in future version of iOS. So we can use `view.window.windowScene.screen`.

**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

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through