2015-06-22 23 views
13

Próbuję dodać niestandardowy pasek UIToolBar do wszystkich moich klawiatur przy tak małej liczbie powtórzeń. Sposób, w jaki obecnie to robię wymaga, aby dodać kod do wszystkich moich viewDidLoads i przypisać każdego delegata textfield do kontrolera viewController, którego używam. Próbowałem tworzyć własną podklasę UIToolBar, ale uważam, że tak naprawdę nie mogę tego zrobić, gdy celem dla przycisków "Gotowe" i "Anuluj" jest widok własny. Czy ktoś ma jakieś sugestie dotyczące tworzenia paska narzędzi, który można łatwo ponownie używać? Z góry dziękuję.Dodaj UIToolBar do wszystkich klawiatur (swift)

override func viewDidLoad() { 
    super.viewDidLoad() 

    var toolBar = UIToolbar() 
    toolBar.barStyle = UIBarStyle.Default 
    toolBar.translucent = true 
    toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1) 
    var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "donePressed") 
    var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelPressed") 
    var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false) 
    toolBar.userInteractionEnabled = true 
    toolBar.sizeToFit() 

    stateField.inputAccessoryView = toolBar 
    stateField.delegate = self 
+0

Podklasa 'UITextField'? Może działa również z rozszerzeniami. – Glorfindel

Odpowiedz

45

Dzięki sugestii Glorfindela i kodowi próbnemu ncerezo rozwiązałem mój problem za pomocą rozszerzeń.

extension UIViewController: UITextFieldDelegate{ 
    func addToolBar(textField: UITextField){ 
     var toolBar = UIToolbar() 
     toolBar.barStyle = UIBarStyle.Default 
     toolBar.translucent = true 
     toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1) 
     var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: "donePressed") 
     var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelPressed") 
     var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
     toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false) 
     toolBar.userInteractionEnabled = true 
     toolBar.sizeToFit() 

     textField.delegate = self 
     textField.inputAccessoryView = toolBar 
    } 
    func donePressed(){ 
     view.endEditing(true) 
    } 
    func cancelPressed(){ 
     view.endEditing(true) // or do something 
    } 
} 

Chociaż nadal potrzebuję wywołać poniższy kod na wszystkich moich polach tekstowych. Czuję, że może istnieć lepszy sposób, bez konieczności wywoływania funkcji na każdym polu tekstowym, ale na razie jest to z pewnością więcej możliwości wielokrotnego użytku.

override func viewDidLoad() { 
    super.viewDidLoad() 
    addToolBar(addressField) 
} 
0

Mam narzędzie, które używam trochę czasu (przeniesiony do Swift z Objective-C), który robi to i trochę więcej, może okazać się przydatne:

https://github.com/ncerezo/SwiftKeyboardAccessory

Tworzy pasek narzędzi z co najmniej przyciskiem "Gotowe", aby odrzucić klawiaturę i opcjonalnie przyciski Następny i Poprzedni. Zajmuje się również wyłączeniem klawiatury po dotknięciu poza polem tekstowym, a także zmianie rozmiaru i przewijaniu widoku, gdy klawiatura pojawia się lub znika. Jest przeznaczony do pracy z UITableVIew, a także z UIScrollView.

12

odpowiednikiem wersji Vivian szybkim 3:

extension UIViewController: UITextFieldDelegate { 
    func addToolBar(textField: UITextField) { 
     let toolBar = UIToolbar() 
     toolBar.barStyle = .default 
     toolBar.isTranslucent = true 
     toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1) 
     let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(donePressed)) 
     let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelPressed)) 
     let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) 
     toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false) 


     toolBar.isUserInteractionEnabled = true 
     toolBar.sizeToFit() 

     textField.delegate = self 
     textField.inputAccessoryView = toolBar 
    } 

    func donePressed() { 
     view.endEditing(true) 
    } 

    func cancelPressed() { 
     view.endEditing(true) // or do something 
    } 
} 
0

Można zrobić pracę podklasy UIToolbar wykorzystując łańcucha reagował. Nie trzeba zmieniać kontrolerów widoku. W Swift 3:

class KeyboardAccessoryToolbar: UIToolbar { 
    convenience init() { 
     self.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50)) 
     self.barStyle = .default 
     self.isTranslucent = true 
     self.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1) 

     let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(self.done)) 
     let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(self.cancel)) 
     let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) 
     self.items = [cancelButton, spaceButton, doneButton] 

     self.isUserInteractionEnabled = true 
     self.sizeToFit() 
    } 

    func done() { 
     // Tell the current first responder (the current text input) to resign. 
     UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) 
    } 

    func cancel() { 
     // Call "cancel" method on first object in the responder chain that implements it. 
     UIApplication.shared.sendAction(#selector(cancel), to: nil, from: nil, for: nil) 
    } 
} 

następnie dodaj następującą do applicationDidFinishLaunching aby zastosować to do wszystkich klawiatur:

let accessoryView = KeyboardAccessoryToolbar() 
    UITextField.appearance().inputAccessoryView = accessoryView 
    UITextView.appearance().inputAccessoryView = accessoryView 
Powiązane problemy