gpt4 book ai didi

ios - 从 TableView 推送 View Controller 后如何保持导航栏标题较大

转载 作者:搜寻专家 更新时间:2023-10-31 23:05:53 24 4
gpt4 key购买 nike

I have a tableview displaying on a main view controller, and when a row is selected a detail view controller is pushed.我有一个主视图 Controller 的大标题,以及详细 View Controller 的一个小/常规标题。它们嵌入在导航 Controller 和标签栏 Controller 中。

Before selecting a row, the main view controller title is large, and when a row is selected the detail view controller title is regular as it should be.但是,当我从详细 View Controller (通过“后退”按钮)返回到主视图 Controller 时,主视图 Controller 上的标题不再大。

我在主视图 Controller 上将“prefersLargeTitles”设置为 true,在详细 View Controller 上将“largeTitleDisplayMode”设置为 never。

我曾尝试在主视图 Controller 上始终将“largeTitleDisplayMode”设置为无济于事。我也试过在任一 View Controller 上将其设置为自动,但似乎没有效果。

我也尝试过使用“viewWillAppear”和“viewWillDisappear”并在其中设置标题,虽然它确实将主视图 Controller 标题重置为大标题,但动画滞后并且不像往常那样流畅从小标题过渡到大标题时。

此外,我对编码还很陌生,这是我在不使用 Storyboard的情况下构建的第一个应用程序,因此代码可能会一团糟。

主视图 Controller 代码:

class HomeViewController: UIViewController {

let tableView = UITableView()

override func loadView() {
super.loadView()
view.backgroundColor = .white
self.title = "Home"

// Set large title
navigationController?.navigationBar.prefersLargeTitles = true


// Make navigation bar transparent
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true


}


extension HomeViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let vc = DetailViewController() as DetailViewController

if indexPath.section == 0 {
vc.detailTitle = itemsTop[indexPath.row]
} else if indexPath.section == 1 {
vc.detailTitle = itemsBottom[indexPath.row]
} else {
print("Failed to load title")
}


navigationController?.pushViewController(vc, animated: true)

tableView.deselectRow(at: indexPath, animated: true)
}

}

详细 View Controller 代码:

class DetailViewController: UIViewController {

var detailTitle: String?

override func viewDidLoad() {
super.viewDidLoad()

title = detailTitle

// Make nav bar transparent
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true

// Prevent large title
navigationController?.navigationBar.prefersLargeTitles = false
}

}

最佳答案

try this it's working. In 'HomeViewController'

override func viewDidLoad() {
super.viewDidLoad()

navigationController?.navigationBar.prefersLargeTitles = true
}

In 'DetailViewController'

override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.largeTitleDisplayMode = .never
}

关于ios - 从 TableView 推送 View Controller 后如何保持导航栏标题较大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57375523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com