2017-10-21 75 views
0

我研究有关最优化方式与UIToolbar互动是UINavigationController的默认集成。如何以编程方式与UINavigationController的集成默认UIToolbar互动?

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

注:所有这一切都无需使用故事板或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.

...蓝色部分对应于我说的,是UINavigationController的集成默认情况下,一个UIToolbar。

这UIToolbar可以替换为自定义实现? ...或者更好的可能是直接互动与此UIToolbar通过我的自定义 UINavigationController和从那里添加所有子视图我需要吗?

+0

不要侵入'UIToolbar',只需在您的'UINavigationController'下放置一个静态视图。 – Sulthan

+0

虽然我不推荐使用'UIToolbar'作为自定义视图,您可以使用自己的自定义工具栏子类实例化一个'UINavigationColler'。 'init(navigationBarClass:AnyClass?,toolbarClass:AnyClass?)'。但它仍然是一个黑客。 –

回答

0

我选择不继承UIToolbar,而是我发现了更多的方便(快速实施),直接与“工具栏”互动财产(以参考内置的工具栏)自带由集成UINavigationController中默认为

我读过这个工具的目的是为客户谁愿意从工具栏提出了一个动作片,也是我不应该直接修改UIToolbar对象和该工具栏的内容的管理是通过关联的自定义视图控制器完成与导航控制器。 (苹果的话的确如此。)

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

这里就是我所做的:在我的UITableViewController的自定义实现我加了一些的UILabel,我直接添加为工具栏子视图...与必要自动布局配置。

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()函数中。

...所以,是的,它的工作原理! ...我们可以将子视图添加到集成在UINavigationController中的内置工具栏参考中。

相关问题