【问题标题】:How to change UINavigationBar background color from clear to red when user scrolls down in the table view当用户在表格视图中向下滚动时,如何将 UINavigationBar 背景颜色从透明更改为红色
【发布时间】:2018-01-24 11:29:05
【问题描述】:

我在它前面有一个表格视图和一个导航栏。 我希望当用户向下滚动时,导航栏的背景颜色开始从透明变为红色,例如增加 alpha。

对我来说的问题是,当导航栏背景 alpha 为 1.0 时用户向下滚动时,用户仍然可以看到导航栏后面!

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    self.mainProfileTableView.contentInset = UIEdgeInsetsMake(-44,0,0,0);


    self.navigationController?.navigationBar.isOpaque = true
    UIApplication.shared.statusBarView?.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)

    self.navigationController?.navigationBar.barTintColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)


    self.navigationController?.navigationBar.setBackgroundImage(UIImage(named : "navigationBarBackGround"), for: .default)

    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.view.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)

}

这是检测用户向上或向下滚动的方法

var lastContentOffset: CGFloat = 0


func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if (self.lastContentOffset < scrollView.contentOffset.y) {
        // moved to top
        print("move Up\(scrollView.contentOffset.y)")
        print("alpha\(Float(scrollView.contentOffset.y / 166))")
        navigationController?.navigationBar.alpha = scrollView.contentOffset.y / 166

    } else if (self.lastContentOffset > scrollView.contentOffset.y) {
        // moved to bottom

        navigationController?.navigationBar.alpha = scrollView.contentOffset.y / 166
        print("alpha\(Float(scrollView.contentOffset.y / 166))")
        print("move down\(scrollView.contentOffset.y)")

    } else {

        // didn't move
    }
}

【问题讨论】:

  • 设置self.navigationController?.navigationBar.isTranslucent = false 去除导航栏的透明度
  • 我设置了但是背景颜色会变黑

标签: ios swift uitableview uiscrollview uinavigationbar


【解决方案1】:

难道导航栏是半透明的? 像这样:

改变它使用:

self.navigationController?.navigationBar.isTranslucent = False

然后删除:

self.navigationController?.navigationBar.setBackgroundImage...
self.navigationController?.navigationBar.shadowImage = UIImage()

然后在用户滚动时修改barTintColor。

【讨论】:

  • 我改变了,但是当我使用它时,导航栏的背景会变黑
  • 我修改了答案,请检查
  • 我使用这个时没有改变
【解决方案2】:

感谢你的帮助但是我应该在滚动视图中使用半透明false 更改方法所以答案是这样的

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (self.lastContentOffset < scrollView.contentOffset.y) {
    // moved to top
    print("move Up\(scrollView.contentOffset.y)")
    print("alpha\(Float(scrollView.contentOffset.y / 166))")
    navigationController?.navigationBar.alpha = scrollView.contentOffset.y / 166
    if Float(scrollView.contentOffset.y / 166) >= 1.0 {

            self.navigationController?.navigationBar.isTranslucent = false

            }

} else if (self.lastContentOffset > scrollView.contentOffset.y) {
    // moved to bottom

    navigationController?.navigationBar.alpha = scrollView.contentOffset.y / 166
    print("alpha\(Float(scrollView.contentOffset.y / 166))")
    print("move down\(scrollView.contentOffset.y)")

} else {

    // didn't move
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    相关资源
    最近更新 更多