【问题标题】:How to programmatically interact with the UIToolbar that UINavigationController integrates by default?如何以编程方式与 UINavigationController 默认集成的 UIToolbar 交互?
【发布时间】:2018-04-02 08:52:50
【问题描述】:

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

我只想以编程方式在我的应用底部添加一个静态视图,那时我才意识到 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

}

...

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

...蓝色部分对应我说的UIToolbar,UINavigationController默认集成的那个。

这个 UIToolbar 可以替换为自定义实现? ...或者也许更好的是通过我的 custom UINavigationController 直接与此 UIToolbar 交互,然后从那里添加我需要的所有子视图

【问题讨论】:

  • 不要入侵UIToolbar,只需在您的UINavigationController 下放置一个静态视图。
  • 虽然我不建议使用UIToolbar 作为自定义视图,但您可以使用自己的自定义工具栏子类实例化UINavigationCollerinit(navigationBarClass: AnyClass?, toolbarClass: AnyClass?)。但它仍然是一个 hack。

标签: ios swift uinavigationcontroller uitoolbar swift4


【解决方案1】:

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

我已经读过这个工具栏是为想要从工具栏显示一个操作表的客户而设计的,我也不应该直接修改 UIToolbar 对象,并且这个工具栏的内容的管理是通过与关联的自定义视图控制器完成的导航控制器。 (确实是苹果的话。)

但在我的特殊情况下,我不需要显示任何操作。我只想使用这个工具栏向用户显示信息,更像是一个状态栏,并最终将其隐藏在特定的视图控制器中:使用布尔属性 isToolbarHidden 很容易实现。

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

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

...应用程序看起来像:

正如您所见,所有这些都发生在 setupToolbarLayout() 函数中。

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

【讨论】:

    猜你喜欢
    • 2016-06-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    相关资源
    最近更新 更多