问题:
我想在滚动tableView时导航栏和大标题保持固定。我如何通过编程方式设置它,以便在滚动tableView时,导航栏不会上下拖动或隐藏大标题?
代码:
override func loadView() {
super.loadView()
setupTableView()
tableView.delegate = self
tableView.dataSource = self
tableView.separatorInset = .zero
tableView.layoutMargins = .zero
self.tableView.layoutMargins = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 0)
self.tableView.contentInset = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 0)
}
func setupTableView() {
view.addSubview(tableView)
tableView.separatorColor = .label
tableView.layer.shadowColor = UIColor.black.cgColor
tableView.layer.shadowOffset = CGSize(width: 0, height: 0)
tableView.backgroundColor = .systemBackground
tableView.tableFooterView = UIView()
tableView.isScrollEnabled = true
//tableView.bounces = false
tableView.showsVerticalScrollIndicator = true
tableView.clipsToBounds = true
let nib = UINib(nibName: "ChatTableViewCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "ChatTableViewCell")
}
func configNavBar() {
navigationController?.navigationBar.barTintColor = .black
navigationController?.navigationBar.backgroundColor = .black
self.navigationController?.navigationBar.backgroundColor = .black
self.title = "Bitcoin"
let navBarAppearance = UINavigationBarAppearance()
//navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.shadowColor = .clear
navBarAppearance.shadowImage = UIImage()
navBarAppearance.backgroundColor = .black
navBarAppearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Log out", style: .done, target: self, action: #selector(didTapLogout))
self.navigationItem.rightBarButtonItem?.tintColor = .systemGray
}
我以编程方式设置表视图如下。我想导航栏保持静态时,表视图滚动。
1条答案
按热度按时间p4tfgftt1#
我用这行简单的代码解决了这个问题,我不知道为什么(我试着搜索,但找不到任何证明这一点的东西),但如果你在视图中添加一个空白的UIView,导航栏仍然是静态的。在viewDidLoad中添加以下行: