Get screen size in Swift as a whole

To position an element random on the screen, but inside the borders I need the resolution / screen size in swift. I already tell you how to get it using CGGraphics. Unfortunately, I need to calculate a little and use it as a UInt32 type, not a CGFloat . I cannot use casting in this way. Any ideas?

 let screenSize: CGRect = UIScreen.mainScreen().bounds let screenWidth = screenSize.width //What I want to do let screenHeight = screenSize.height-arc4random_uniform(screenSize.height) 
+7
ios swift
source share
4 answers

If you need random X and Y positions for your element, you can do something like this:

 let screenSize = UIScreen.main.bounds let randomXPos = CGFloat(arc4random_uniform(UInt32(screenSize.width))) let randomYPos = CGFloat(arc4random_uniform(UInt32(screenSize.height))) 
+9
source share

To get the direct value in Int, use this code

 var bounds: CGRect = UIScreen.mainScreen().bounds var w:Int = Int(self.bounds.size.width) var h:Int = Int(self.bounds.size.height) 

All you have to do is convert the return value from CGFloat to Integer

Use the following code to get the screen height width value and then convert CGFloat to Int

 var bounds: CGRect = UIScreen.mainScreen().bounds var width:CGFloat = bounds.size.width var height:CGFloat = bounds.size.height 
+3
source share

are you trying to do it

 let screenSize: CGRect = UIScreen.mainScreen().bounds let screenWidth = screenSize.width let screenHeight = screenSize.height let screenWidth = screenSize.width * 0.75 
+2
source share

I think this is what you need:

 let screenHeight = screenSize.height - CGFloat(arc4random_uniform(UInt32(screenSize.height))) 
+1
source share

All Articles