为什么NSLog将屏幕宽度和屏幕高度显示为0?

时间:2015-04-06 06:14:11

标签: ios swift

所以我写下了一段代码,它按照我的意图在中间显示红色方块。但是,当我“NSLog”将设备屏幕尺寸的输出编辑到控制台上时,我发现该值为0.为什么会这样?

let container = UIView()
let redSquare = UIView()
let blueSquare = UIView()
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let screenSize : CGSize = UIScreen.mainScreen().applicationFrame.size
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height

    NSLog("screenWidth = %f, screenHeight = %f", screenWidth, screenHeight);
    self.container.frame = CGRect(x: 50, y: 50, width: screenWidth-100, height: screenWidth-100)

    self.view.addSubview(container)

    // set red square frame up
    // we want the blue square to have the same position as redSquare
    // so lets just reuse blueSquare.frame
    self.redSquare.frame = CGRect(x: 0, y: 0, width: screenWidth-100, height: screenWidth-100)
    self.blueSquare.frame = redSquare.frame

    // set background colors
    self.redSquare.backgroundColor = UIColor.redColor()
    self.blueSquare.backgroundColor = UIColor.blueColor()

    // for now just add the redSquare
    // we'll add blueSquare as part of the transition animation
    self.container.addSubview(self.redSquare)

}

0 个答案:

没有答案