gpt4 book ai didi

ios - 如何以编程方式与 UINavigationController 默认集成的 UIToolbar 进行交互?

转载 作者:行者123 更新时间:2023-11-30 12:04:08 24 4
gpt4 key购买 nike

我正在研究与 UINavigationController 默认集成的 UIToolbar 交互的最佳方式。

我只想以编程方式在我的应用程序底部添加一个静态 View ,就在那时我刚刚意识到 UINavigationController 已经集成了一个 UIToolbar。

注意:所有这些都无需使用 Storyboard 或 Xib 文件。

AppDelegate.swift:

...

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

window = UIWindow(frame: UIScreen.main.bounds)

window?.makeKeyAndVisible()

window?.rootViewController = MyNavigationController(rootViewController: MyTableViewController())

return true

}

...

我展示这段代码是为了让您了解到目前为止我所遵循的方法......这就是我的测试应用程序现在的样子:

Application example where you can see the UIToolbar (the blue segment) that comes integrated in UINavigationController by default.

...蓝色部分对应于我正在谈论的 UIToolbar,即 UINavigationController 默认集成的 UIToolbar。

这个 UIToolbar 可以用自定义实现替换吗? ...或者也许更好的是通过我的自定义 UINavigationController 与此 UIToolbar 直接交互,并从那里添加我需要的所有 subview

最佳答案

我选择不子类化 UIToolbar,但我发现直接与“toolbar”属性(对内置工具栏的引用)进行交互更方便(快速实现), 默认集成在 UINavigationController 中。

我读到此工具栏适用于想要从工具栏显示操作表的客户,而且我不应该直接修改 UIToolbar 对象,并且此工具栏内容的管理是通过与关联的自定义 View Controller 完成的导航 Controller 。 (确实是苹果的话。)

但在我的特殊情况下,我不需要显示任何操作。我只是想使用此工具栏向用户显示信息,更像是状态栏,并最终将其隐藏在特定的 View Controller 中:使用 bool 属性 isToolbarHidden 可以轻松实现这一点。

这就是我所做的:在 UITableViewController 的自定义实现中,我添加了一些 UILabel,我直接将其添加为工具栏的 subview ...使用必要自动布局配置。

MyTableViewController.swift:

import UIKit

class MyTableViewController: UITableViewController {

let monthlyLabel: UILabel = {

let label = UILabel()

label.translatesAutoresizingMaskIntoConstraints = false

label.font = UIFont.boldSystemFont(ofSize: 14)

label.sizeToFit()

label.textAlignment = .left

label.text = "Monthly:"

return label

}()

let fixedMonthlyExpenses: UILabel = {

let label = UILabel()

label.translatesAutoresizingMaskIntoConstraints = false

label.font = UIFont.systemFont(ofSize: 14)

label.sizeToFit()

label.textAlignment = .left

return label

}()

let annuallyLabel: UILabel = {

let label = UILabel()

label.translatesAutoresizingMaskIntoConstraints = false

label.font = UIFont.boldSystemFont(ofSize: 14)

label.sizeToFit()

label.textAlignment = .right

label.text = "Annually:"

return label

}()

let fixedAnnuallyExpenses: UILabel = {

let label = UILabel()

label.translatesAutoresizingMaskIntoConstraints = false

label.font = UIFont.systemFont(ofSize: 14)

label.sizeToFit()

label.textAlignment = .right

return label

}()

...

override func viewDidLoad() {

super.viewDidLoad()

navigationItem.title = "My Test"

tableView.backgroundColor = UIColor(displayP3Red: 38/255, green: 38/255, blue: 38/255, alpha: 1)

tableView.tableFooterView = UIView()

...

navigationController?.isToolbarHidden = false

let monthlyLabel: NSNumber = 9.99
let annuallyLabel: NSNumber = 119.88

let numberFormatter = NumberFormatter()

numberFormatter.numberStyle = .currency

fixedMonthlyExpenses.text = numberFormatter.string(from: monthlyLabel)
fixedAnnuallyExpenses.text = numberFormatter.string(from: annuallyLabel)

setupToolbarLayout()

} // viewDidLoad

...

func setupToolbarLayout() {

guard let toolbar = navigationController?.toolbar else {

return

} // guard

toolbar.addSubview(monthlyLabel)

monthlyLabel.leadingAnchor.constraint(equalTo: toolbar.leadingAnchor, constant: 5).isActive = true
monthlyLabel.centerYAnchor.constraint(equalTo: toolbar.centerYAnchor).isActive = true

toolbar.addSubview(fixedMonthlyExpenses)

fixedMonthlyExpenses.leadingAnchor.constraint(equalTo: monthlyLabel.trailingAnchor, constant: 5).isActive = true
fixedMonthlyExpenses.centerYAnchor.constraint(equalTo: toolbar.centerYAnchor).isActive = true

toolbar.addSubview(fixedAnnuallyExpenses)

fixedAnnuallyExpenses.trailingAnchor.constraint(equalTo: toolbar.trailingAnchor, constant: -5).isActive = true
fixedAnnuallyExpenses.centerYAnchor.constraint(equalTo: toolbar.centerYAnchor).isActive = true

toolbar.addSubview(annuallyLabel)

annuallyLabel.trailingAnchor.constraint(equalTo: fixedAnnuallyExpenses.leadingAnchor, constant: -5).isActive = true
annuallyLabel.centerYAnchor.constraint(equalTo: toolbar.centerYAnchor).isActive = true

} // setupToolbarLayout

...

} // MyTableViewController

...应用程序如下所示:

Shows an UIToolbar with some UILabel as subviews

如您所见,这一切都发生在 setupToolbarLayout() 函数内。

...所以,是的,它有效! ...我们可以将 subview 添加到集成在 UINavigationController 中的内置工具栏引用中。

关于ios - 如何以编程方式与 UINavigationController 默认集成的 UIToolbar 进行交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46861718/

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