如何着色状态栏?

时间:2016-03-04 17:33:25

标签: ios swift uinavigationbar

我如何着色状态栏我有一个导航栏,当我滚动我的桌子但单元格进入状态栏时会消失...

这里有我的导航控制器

class NavigationController : UINavigationController
{
    override func viewDidLoad()
    {
          super.viewDidLoad()
          UINavigationBar.appearance().barTintColor = Utility().coloreTitolo()
          UINavigationBar.appearance().translucent = false;
          UINavigationBar.appearance().tintColor = UIColor.whiteColor()
          navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
}

我尝试使用此代码,但当我向下滚动视图时也会显示...

   func scrollViewDidScroll(scrollView: UIScrollView)
{
    let statusBar = UIView(frame:
        CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0)
    )
    statusBar.backgroundColor = Utility().coloreTitolo()
    statusBar.tag = 100;

    if(scrollView.panGestureRecognizer.translationInView(scrollView).y > 0)
    {
        self.view.addSubview(statusBar)
    }
    else
    {
        let subViews = self.navigationController?.navigationBar.subviews
        for subview in subViews!
        {
            if (subview.tag == 100) {
                subview.removeFromSuperview()
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

I couldn't properly understand the wording of your question, but from what I gathered you're wanting to make the background of your UINavigationBar white, so you can't see objects passing behind it.

Try adding this to your ViewController(s) in the viewWillAppear() function:

    let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
    view.backgroundColor = UIColor.whiteColor()

    self.view.addSubview(view)

Tell me if that works for you.

Edit: Here's what I've put together, however I haven't tried it so I don't know the outcome.

override func viewDidLoad() {
    super.viewDidLoad()

    NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "testForHidden", userInfo: nil, repeats: true)
}

func testForHidden() {

    let fakebar = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
    fakebar.backgroundColor = UIColor.whiteColor()

    switch navigationController?.navigationBarHidden {
    case true?:
        self.view.addSubview(fakebar)
    case false?:
        fakebar.removeFromSuperview()
    default: break
    }

}

答案 1 :(得分:1)

The status bar has no color: it is transparent. If you want a color to appear through the status bar, put a view with that color at the spot where the status bar will be (that is, the top 20 pixels of your main view).

Notice that when the navigation bar goes away in this screen cast, the background color shows through the status bar:

enter image description here

That's done entirely with constraint configuration in the storyboard; no code was needed. The top of the table view is effectively pinned to the bottom of the top layout guide, so it stops at the status bar and we see the background color behind it, coloring the status bar.

(However, with a table view the usual approach is to adjust the contentInset (and scrollIndicatorInsets. The row content will still scroll behind the status bar, but when you're scrolled all the way the top row will be entirely visible below it.)

答案 2 :(得分:1)

与Charles Truluck的答案类似,你可以这样做:

if navigationBar.hidden == false {
    let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.mainScreen().bounds.size.width, height: 20.0))
    view.backgroundColor = UIColor.whiteColor()

    self.view.addSubview(view)
}