ios - 状态栏颜色 - iphone风景问题

时间:2016-03-29 15:37:45

标签: ios iphone

我尝试使用它来更改状态栏颜色,在肖像中工作正常,

let proxyViewForStatusBar : UIView = UIView(frame: CGRectMake(0, 0,self.view.frame.size.width, 20))     
proxyViewForStatusBar.backgroundColor=UIColor.purpleColor()
self.view.addSubview(proxyViewForStatusBar)

我不想在iphone上使用它。

所以我试过这个

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {


    let proxyViewForStatusBar : UIView = UIView(frame: CGRectMake(0, 0,self.view.frame.size.width, 20))



    if UIDevice.currentDevice().orientation.isLandscape.boolValue && UIDevice.currentDevice().userInterfaceIdiom == .Phone{

        proxyViewForStatusBar.backgroundColor = UIColor.clearColor()
    }else {
        proxyViewForStatusBar.backgroundColor=UIColor.purpleColor()
    }

    self.view.addSubview(proxyViewForStatusBar)

}

但结果很尴尬,状态栏部分显示。enter image description here已附加。

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我看到你已经添加了两次viewProxy。第一次罚款。但是当你旋转另一个视图时,最后一个视图仍在这里。

因此,您可以为视图创建全局:

 var proxyViewForStatusBar : UIView!

ViewDidload

proxyViewForStatusBar= UIView(frame: CGRectMake(0, 0,self.view.frame.size.width, 20))     
proxyViewForStatusBar.backgroundColor=UIColor.purpleColor()
self.view.addSubview(proxyViewForStatusBar)
旋转时

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
    proxyViewForStatusBar.frame = CGRectMake(0, 0,self.view.frame.size.width, 20)
    if UIDevice.currentDevice().orientation.isLandscape.boolValue && UIDevice.currentDevice().userInterfaceIdiom == .Phone{

        proxyViewForStatusBar.backgroundColor = UIColor.clearColor()
    }else {
        proxyViewForStatusBar.backgroundColor=UIColor.purpleColor()
    }

}