2017-10-21 176 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中的內置工具欄參考中。

相關問題