如何让UITableView每次加载时默认滚动到顶部并显示大导航标题
解决方案
核心修改点
- 每个子页面单独配置大标题显示模式
不要把大标题的全局配置和单个页面的显示模式混写,在第一个列表页和跳转后的FlowTableViewController的viewDidLoad方法中分别添加以下代码:
navigationItem.largeTitleDisplayMode = .always navigationController?.navigationBar.prefersLargeTitles = true
- 第二个页面加载时主动滚动到顶部触发大标题展开
在FlowTableViewController的viewWillAppear方法中添加滚动到顶部的逻辑,强制大标题完全展开:
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) tableView.setContentOffset(.zero, animated: false) }
可选优化:适配新版iOS导航栏样式配置
你当前直接修改navigationBar.backgroundColor、barTintColor的写法在iOS13+容易出现样式异常,建议统一用UINavigationBarAppearance全局配置导航栏样式,可将以下代码放到AppDelegate启动方法或者公共样式配置类中:
if #available(iOS 13.0, *) { let appearance = UINavigationBarAppearance() appearance.configureWithOpaqueBackground() // 替换为你自己的色值和字体配置 appearance.backgroundColor = UIColor(hexString: vc1.defaults.string(forKey: "darkest")) appearance.titleTextAttributes = [ .foregroundColor: UIColor(hexString: vc1.defaults.string(forKey: "almostWhite")), .font: UIFont(name: "Advert-Bold", size: setSmallTitleSize())! ] appearance.largeTitleTextAttributes = [ .foregroundColor: UIColor(hexString: vc1.defaults.string(forKey: "almostWhite")), .font: UIFont(name: "Advert-Bold", size: setLargeTitleSize())! ] UINavigationBar.appearance().standardAppearance = appearance UINavigationBar.appearance().scrollEdgeAppearance = appearance }
修改后即可实现每个列表页加载时默认展示大标题,不会继承上一页的收缩状态。
内容的提问来源于stack exchange,提问作者Julian Frank
相关产品推荐
相关产品推荐

