【问题标题】:How to color status bar?如何为状态栏着色?
【发布时间】:2016-03-04 17:33:24
【问题描述】:

我如何为状态栏着色 我有一个导航栏,当我滚动表格但单元格在状态栏下方时消失...

这是我的导航控制器

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

【问题讨论】:

  • 您是否要为状态栏中的文本着色?

标签: ios swift uinavigationbar


【解决方案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)
}

【讨论】:

    【解决方案2】:

    状态栏没有颜色:它是透明的。如果您希望通过状态栏显示颜色,请将具有该颜色的视图放在状态栏所在的位置(即主视图的前 20 个像素)。

    请注意,当导航栏在此截屏中消失时,背景颜色会通过状态栏显示:

    这完全是通过情节提要中的约束配置完成的;不需要代码。表格视图的顶部有效地固定在顶部布局指南的底部,因此它停在状态栏上,我们看到它后面的背景颜色,为状态栏着色。

    (但是,对于表格视图,通常的方法是调整contentInset(和scrollIndicatorInsets。行内容仍将滚动到状态栏后面,但是当您一直滚动到顶行时在其下方完全可见。)

    【讨论】:

    • 我遇到了和#Charles 说的同样的问题...:/ 如何让这个视图仅在我的导航栏“隐藏”时出现?
    • #matt 我怎样才能改变我的指标插入和内容插入以使其工作?
    • 是的,我明白了,但这不是我需要的……但是您可以在 viewDidAppear 中添加此“navigationController?.hidesBarsOnSwipe = true”以在滚动时隐藏导航栏
    • 给我一些代码...我还有一个隐藏状态栏的导航栏
    • 正如我在回答中所说,没有代码。这一切都是通过情节提要中的约束完成的。
    【解决方案3】:

    我无法正确理解您问题的措辞,但根据我收集到的信息,您希望将 UINavigationBar 的背景设置为白色,这样您就看不到它后面经过的物体。

    尝试在 viewWillAppear() 函数中将其添加到您的 ViewController(s):

        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)
    

    告诉我这是否适合你。

    编辑: 这是我整理的,但我没有尝试过,所以我不知道结果。

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

    【讨论】:

    • 它可以工作,但是当我不向上滚动时,我在导航栏下看到了这个视图...我希望这个视图仅在导航栏“隐藏”时出现
    • @MarcoCastano 你总是可以使用NSTimer 来运行一个函数来检查你是否滚动过去一个点,if 你已经到达那个点然后使用if在同一个函数中运行代码的语句。
    • 你能写一些代码吗?我添加并编辑但无法弄清楚...:/
    • 嘿,@MarcoCastano 你知道了吗?对不起,我已经离开了。
    • @MarcoCastano 我会为你准备一些东西。给我几分钟。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2016-12-16
    相关资源
    最近更新 更多