【问题标题】:My navigation bar is not moving up when scrolling UITableView with background image滚动带有背景图像的 UITableView 时,我的导航栏没有向上移动
【发布时间】:2020-09-09 09:22:08
【问题描述】:

我在 UINavigationController 中嵌入的视图上有这个视图层次结构:

当我滚动 UITableView 时,导航栏没有向上移动(标题没有变小)它保持这样的状态:

如果我将图像视图删除为背景视图,一切正常。

我的导航是这样配置的:

navigationItem.title = "Title here"
navigationItem.largeTitleDisplayMode = .always

navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.barStyle = UIBarStyle.blackTranslucent
navigationController?.navigationBar.backgroundColor = .clear

此处提供了一个演示该问题的项目: https://drive.google.com/file/d/181Aggala2ZfGN0lDjEtHWg0vobkM0iJc/view?usp=sharing

我已经尝试更改 tableview 的插图,但没有成功。

tableView.contentInset = UIEdgeInsets(top: navigationController?.navigationBar.height, left: 0, bottom: 0, right: 0)

谢谢!

【问题讨论】:

    标签: ios uitableview uiscrollview uinavigationcontroller uinavigationbar


    【解决方案1】:

    如您所见,要使大标题字体按您的意愿工作,UIScrollView 或其任何子类必须是视图层次结构中的第一个元素。

    为了解决您的问题,您可以尝试将背景图片直接设置为UITableView的背景。

    编辑: 好的,根据您的评论,您需要包括导航栏在内的所有内容的背景。有一种方法可以实现这一点,那就是将UINavigationControllerviewDidLoad 内部子类化:

    override func viewDidLoad() {
       super.viewDidLoad()
    
       let image = UIImage(named: "wallpaper")
       let imageView = UIImageView(image: image)
       imageView.contentMode = .scaleAspectFill
    
       imageView.frame = UIScreen.main.bounds
       //this below part is important. Make sure to set it to 0 otherwise it will cover everything else
       view.insertSubview(imageView, at: 0)
    }
    

    然后确保包含UITableViewUIViewController 具有清晰的UIView 颜色,并从UIViewController 中删除背景图像

    【讨论】:

    • 但是我需要图像覆盖整个屏幕(我需要在导航栏和表格视图后面看到这个图像)。如果我将图像添加为背景图像,我只会在表格视图后面看到它。而且如果我把table view放在导航栏后面,table view的内容也会在导航栏后面,只需要图片在后面。见这里:imgur.com/a/8KVyHMf
    • @user1539434 请查看编辑。它应该可以解决您的问题
    猜你喜欢
    • 2019-11-30
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多