使用背景图片滚动 UITableView 时,我的导航栏没有向上移动

My navigation bar is not moving up when scrolling UITableView with background image

我在 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 的 insets 但它没有用。

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

谢谢!

正如您发现的那样,要使大标题字体按您希望的方式工作,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

的背景图像