当滚动UITableView时,我的导航栏不能向上移动,并有背景图片。

问题描述 投票:0回答:1

我在一个嵌入到UINavigationController中的视图上有这样的视图层次结构。

enter image description here

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

enter image description here

如果我把图片视图作为背景视图移除,一切都能正常工作。

enter image description here

我的导航是这样配置的。

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.comfiled181Aggala2ZfGN0lDjEtHWg0vobkM0iJcview?usp=sharing。

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

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

谢谢!我在一个嵌入在UINavigationController中的视图上有这样的视图层次结构

ios uitableview uiscrollview uinavigationcontroller uinavigationbar
1个回答
1
投票

正如你所发现的那样,为了使大标题字体按照你的要求工作,你可以使用 UIScrollView 或其任何子类需要成为视图层次结构中的第一个元素。

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

编辑。 好吧,根据你的评论,你希望所有的东西都有一个背景,包括导航栏。有一种方法可以实现这一点,那就是将你的 UINavigationController 里边 viewDidLoad:

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

然后确保你的 UIViewController 含有 UITableView 有一个清晰的颜色为 UIView 并删除该背景图片 UIViewController

© www.soinside.com 2019 - 2024. All rights reserved.