Ricky Gore
Ricky Gore

Reputation: 13

Xcode / Swift / UIScreen.main.bounds.size.height Returning 0

The code below is returning 0. I've tried all the samples I can find, but it still always returns 0. I'm on XCode 9.4.1

    // Screen Size
    let screenSize = UIScreen.main.bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    NSLog("Screen Height is %i", screenHeight);
    NSLog("Screen Width is %i", screenWidth);
    // Screen Size

Upvotes: 0

Views: 791

Answers (2)

user8350399
user8350399

Reputation:

Just print the statement with print()

print("Screen Height is \(screenHeight)")
print("Screen Width is \(screenWidth)")

In your case, %i is requesting an integer and screenHeight and screenWidthare floats. Therefore use %f

NSLog("Screen Height is %f", screenHeight);
NSLog("Screen Width is %f", screenWidth);

Upvotes: 1

Jack
Jack

Reputation: 14369

Use print()

print("Screen Height is", screenHeight)
print("Screen Width is", screenWidth)

or by %f

NSLog("Screen Height is %f", screenHeight)
NSLog("Screen Width is %f", screenWidth)

Upvotes: 0

Related Questions