在单独的视图控制器上使用 scrollViewDidScroll 时无法更改 UINavigationBar 的样式 [英] Cannot change style of UINavigationBar when using scrollViewDidScroll on separate View Controller

查看:24
本文介绍了在单独的视图控制器上使用 scrollViewDidScroll 时无法更改 UINavigationBar 的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UINavigationController 中嵌入了三个视图控制器.FirstViewController 导航到 SecondTableViewController,后者导航到 ThirdDetailViewController.

I have three view controllers embedded within a UINavigationController. FirstViewController navigates to SecondTableViewController, which navigates to ThirdDetailViewController.

我遇到的问题是在 SecondTableViewController 中使用 scrollViewDidScroll 方法自定义 status barUINavigationBar 的样式时 它还覆盖堆栈中的其他视图控制器以及所有状态栏样式.

The problem I am having is when customising the style of the status bar and UINavigationBar using the scrollViewDidScroll method within the SecondTableViewController it also override the other view controllers within the stack that come after it as well as all status bar styles.

有人知道如何防止 scrollViewDidScroll 方法影响堆栈中的其他视图控制器吗?

Does anybody know how I can prevent the scrollViewDidScroll method from effecting other view controllers within the stack ?

FirstViewController

override func viewWillAppear(animated: Bool) {

UIApplication.sharedApplication().statusBarStyle = .Default
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarPosition: .Any, barMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()       
self.navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
self.navigationController?.navigationBar.tintColor = UIColor.blackColor()
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = true

}

SecondTableViewController

我根据用户向下滚动的程度来更改导航的样式以融入内容或从内容中脱颖而出.

I change the style of the navigation depending on how far down the user has scrolled to either blend in or stand out from the content.

其他视图控制器的样式更改为方法中设置的任何样式.

The style of other view controllers change to whichever style is set in the method.

 override func scrollViewDidScroll(scrollView: UIScrollView) {

    let color = colorWheel()

    if (scrollView.contentOffset.y > -60) {

        UIApplication.sharedApplication().statusBarStyle = .Default

        self.navigationController?.navigationBar.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
        self.navigationController?.navigationBar.tintColor = color.appColor
        self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
        self.navigationController?.view.backgroundColor = color.appColor
        self.navigationController?.navigationBar.translucent = true

    } else {

        UIApplication.sharedApplication().statusBarStyle = .LightContent

        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.backgroundColor = UIColor.clearColor()
        self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
        self.navigationController?.navigationBar.barTintColor = UIColor.clearColor()
        self.navigationController?.view.backgroundColor = UIColor.clearColor()
        self.navigationController?.navigationBar.translucent = true
    }
}

ThirdDetailViewController

override func viewWillAppear(animated: Bool) {

UIApplication.sharedApplication().statusBarStyle = .Default
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarPosition: .Any, barMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()       
self.navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
self.navigationController?.navigationBar.tintColor = UIColor.blackColor()
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = true

}

推荐答案

也在 viewDidAppear 方法中更改导航属性.

Change the Navigation properties from within the viewDidAppear method as well.

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

UIApplication.sharedApplication().statusBarStyle = .Default
UIApplication.sharedApplication().statusBarStyle = .Default
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarPosition: .Any, barMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()       
self.navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
self.navigationController?.navigationBar.tintColor = UIColor.blackColor()
self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = true

}

这篇关于在单独的视图控制器上使用 scrollViewDidScroll 时无法更改 UINavigationBar 的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆