简体   繁体   中英

ios - status bar color - iphone landscape issue

i tried using this to change the status bar color, works fine in portrait,

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

And i don't want it in landscape on iphone.

so i tried this

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)

}

but the result is awkward, the status bar shows partially. 在此处输入图片说明 Attached.

Any help is appreciated.

I see that you have added viewProxy two times. First time it work fines. But when you rotate another view added and last view still here.

So you can create a global for view:

 var proxyViewForStatusBar : UIView!

In ViewDidload :

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

when rotate:

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()
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM