Xcode / Swift / UIScreen.main.bounds.size.height 返回 0

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

下面的代码返回 0。我已经尝试了我能找到的所有样本,但它仍然总是 returns 0。我在 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

使用print()

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

%f

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

只需打印带有 print()

的语句
print("Screen Height is \(screenHeight)")
print("Screen Width is \(screenWidth)")

在您的例子中,%i 请求的是整数,而 screenHeightscreenWidth 是浮点数。因此使用 %f

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