Add keyboard done button using global functionHow can I make a UITextField move up when the keyboard is present - on starting to edit?How to navigate through textfields (Next / Done Buttons)“unrecognized selector” error when trying to dismiss keyboardAdding validation method on the 'Done' button added to the UITextField input methodUIPicker subview not recognizing inputAdd UIToolBar to all keyboards (swift)Is it possible to add a Done key to ALL keyboards in an app?Add UILabel to UIToolbar ProgrammaticallyDismissing keyboard when user taps outside of UITextFieldCan't dismiss KeyBoard when a Done button pressed in UITableViewCell

Stopping power of mountain vs road bike

Why doesn't H₄O²⁺ exist?

Modeling an IP Address

Why is the 'in' operator throwing an error with a string literal instead of logging false?

Fully-Firstable Anagram Sets

Why can't we play rap on piano?

Western buddy movie with a supernatural twist where a woman turns into an eagle at the end

Why do I get two different answers for this counting problem?

In Romance of the Three Kingdoms why do people still use bamboo sticks when paper had already been invented?

Why is consensus so controversial in Britain?

Why do bosons tend to occupy the same state?

What does it mean to describe someone as a butt steak?

Why are electrically insulating heatsinks so rare? Is it just cost?

Anagram holiday

Is it legal for company to use my work email to pretend I still work there?

What is a clear way to write a bar that has an extra beat?

Facing a paradox: Earnshaw's theorem in one dimension

Is it possible to run Internet Explorer on OS X El Capitan?

Why does Kotter return in Welcome Back Kotter?

What's the point of deactivating Num Lock on login screens?

SSH "lag" in LAN on some machines, mixed distros

How to prevent "they're falling in love" trope

Brothers & sisters

What mechanic is there to disable a threat instead of killing it?



Add keyboard done button using global function


How can I make a UITextField move up when the keyboard is present - on starting to edit?How to navigate through textfields (Next / Done Buttons)“unrecognized selector” error when trying to dismiss keyboardAdding validation method on the 'Done' button added to the UITextField input methodUIPicker subview not recognizing inputAdd UIToolBar to all keyboards (swift)Is it possible to add a Done key to ALL keyboards in an app?Add UILabel to UIToolbar ProgrammaticallyDismissing keyboard when user taps outside of UITextFieldCan't dismiss KeyBoard when a Done button pressed in UITableViewCell






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am trying to figure out how I can create a global function that adds a done button to keyboards I choose.



I can add a done button, but I currently copy the code to each view controllor. I want to add a function to the UITextField to show the done button on the keyboard.
Current code commonly found on stack overflow.



Code found in viewDidLoad:



let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneBotton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(self.doneClicked))

toolbar.setItems([doneBotton], animated: false)


@objc found outside viewdidload:



@objc func doneClicked() 
view.endEditing(true)










share|improve this question






























    1















    I am trying to figure out how I can create a global function that adds a done button to keyboards I choose.



    I can add a done button, but I currently copy the code to each view controllor. I want to add a function to the UITextField to show the done button on the keyboard.
    Current code commonly found on stack overflow.



    Code found in viewDidLoad:



    let toolbar = UIToolbar()
    toolbar.sizeToFit()
    let doneBotton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(self.doneClicked))

    toolbar.setItems([doneBotton], animated: false)


    @objc found outside viewdidload:



    @objc func doneClicked() 
    view.endEditing(true)










    share|improve this question


























      1












      1








      1








      I am trying to figure out how I can create a global function that adds a done button to keyboards I choose.



      I can add a done button, but I currently copy the code to each view controllor. I want to add a function to the UITextField to show the done button on the keyboard.
      Current code commonly found on stack overflow.



      Code found in viewDidLoad:



      let toolbar = UIToolbar()
      toolbar.sizeToFit()
      let doneBotton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(self.doneClicked))

      toolbar.setItems([doneBotton], animated: false)


      @objc found outside viewdidload:



      @objc func doneClicked() 
      view.endEditing(true)










      share|improve this question
















      I am trying to figure out how I can create a global function that adds a done button to keyboards I choose.



      I can add a done button, but I currently copy the code to each view controllor. I want to add a function to the UITextField to show the done button on the keyboard.
      Current code commonly found on stack overflow.



      Code found in viewDidLoad:



      let toolbar = UIToolbar()
      toolbar.sizeToFit()
      let doneBotton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(self.doneClicked))

      toolbar.setItems([doneBotton], animated: false)


      @objc found outside viewdidload:



      @objc func doneClicked() 
      view.endEditing(true)







      ios swift






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 0:36









      rmaddy

      246k27327390




      246k27327390










      asked Mar 8 at 0:17









      k.thomask.thomas

      266




      266






















          5 Answers
          5






          active

          oldest

          votes


















          2














          you could simply create an extension of UITextField like below:-



          extension UITextField 

          func addDoneButton()
          let toolbar = UIToolbar()
          toolbar.sizeToFit()

          let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneClicked))

          toolbar.setItems([doneButton], animated: false)

          self.inputAccessoryView = toolbar


          @objc func doneClicked()
          self.endEditing(true)




          And call it like below:-



          class DoneKeyboardViewController: UIViewController 

          @IBOutlet weak var txtField: UITextField!

          override func viewDidLoad()
          super.viewDidLoad()

          txtField.addDoneButton()







          share|improve this answer






























            1














            you can try this extension of UITextField.



            the UITextField's extension is global, the TextFieldDone protocol is used for tagging where you add keyboards you choose.



            class ViewController: UIViewController, TextFieldDone 
            @IBOutlet weak var textField: UITextField!

            override func viewDidLoad()
            super.viewDidLoad()
            textField.addDoneButtonOnKeyboard(self)


            @objc
            func doneButtonAction(_ textField: UITextField)
            print("this")



            protocol TextFieldDone
            func doneButtonAction(_ textField: UITextField)


            extension UITextField: TextFieldDone

            func addDoneButtonOnKeyboard(_ target: UIViewController)

            let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
            doneToolbar.barStyle = UIBarStyle.blackTranslucent
            let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
            let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: target, action: #selector(doneButtonAction(_:)))

            var items = [UIBarButtonItem]()
            items.append(flexSpace)
            items.append(done)
            doneToolbar.items = items
            doneToolbar.sizeToFit()
            inputAccessoryView = doneToolbar


            @objc func doneButtonAction(_ textField: UITextField)
            print("PlaceHolder")







            share|improve this answer
































              0














              You could create a new view controller class that implements this functionality, then use it as a super class for the rest of your view controllers






              share|improve this answer






























                0














                You could subclass UIViewController and override its viewDidLoad with the implementation you've presented and then set all of your view controller classes in interface builder to your derived class instead of UIViewController.



                Or you could subclass UITextField and set its keyboard to the Done keyboard as a default in its init method with:



                class TextField : UITextField 
                required init?(coder aDecoder: NSCoder)
                super.init(coder: aDecoder)
                self.returnKeyType = .done




                Then everywhere in your storyboards where you have a text field that you want to show the done button, you set the text field's class to TextField instead of UITextField. You need the initWithCoder because that's the initializer that gets called when creating the class from a storyboard.






                share|improve this answer






























                  0














                  Try this :



                  class CustomTextField: UITextField 
                  override init(frame: CGRect)
                  super.init(frame: frame)
                  addDoneButtonOnKeyboardToInput()

                  required init?(coder aDecoder: NSCoder)
                  super.init(coder: aDecoder)
                  addDoneButtonOnKeyboardToInput()

                  var doneToolbar: UIToolbar?
                  var doneButton: UIBarButtonItem?
                  func addDoneButtonOnKeyboardToInput()
                  if (doneToolbar == nil)
                  doneToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
                  doneToolbar?.barStyle = UIBarStyle.default

                  let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
                  doneButton = UIBarButtonItem(title: SF.buttons.done, style: UIBarButtonItem.Style.done, target: nil, action: #selector(doneButtonAction(sender:)))

                  var items = [UIBarButtonItem]()
                  items.append(flexSpace)
                  if let btn = doneButton
                  items.append(btn)


                  doneToolbar?.items = items

                  doneButton?.target = self
                  doneToolbar?.sizeToFit()

                  self.inputAccessoryView = doneToolbar



                  @objc
                  func doneButtonAction(sender: UIBarButtonItem)
                  self.resignFirstResponder()







                  share|improve this answer























                    Your Answer






                    StackExchange.ifUsing("editor", function ()
                    StackExchange.using("externalEditor", function ()
                    StackExchange.using("snippets", function ()
                    StackExchange.snippets.init();
                    );
                    );
                    , "code-snippets");

                    StackExchange.ready(function()
                    var channelOptions =
                    tags: "".split(" "),
                    id: "1"
                    ;
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function()
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled)
                    StackExchange.using("snippets", function()
                    createEditor();
                    );

                    else
                    createEditor();

                    );

                    function createEditor()
                    StackExchange.prepareEditor(
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader:
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    ,
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    );



                    );













                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054907%2fadd-keyboard-done-button-using-global-function%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown

























                    5 Answers
                    5






                    active

                    oldest

                    votes








                    5 Answers
                    5






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    2














                    you could simply create an extension of UITextField like below:-



                    extension UITextField 

                    func addDoneButton()
                    let toolbar = UIToolbar()
                    toolbar.sizeToFit()

                    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneClicked))

                    toolbar.setItems([doneButton], animated: false)

                    self.inputAccessoryView = toolbar


                    @objc func doneClicked()
                    self.endEditing(true)




                    And call it like below:-



                    class DoneKeyboardViewController: UIViewController 

                    @IBOutlet weak var txtField: UITextField!

                    override func viewDidLoad()
                    super.viewDidLoad()

                    txtField.addDoneButton()







                    share|improve this answer



























                      2














                      you could simply create an extension of UITextField like below:-



                      extension UITextField 

                      func addDoneButton()
                      let toolbar = UIToolbar()
                      toolbar.sizeToFit()

                      let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneClicked))

                      toolbar.setItems([doneButton], animated: false)

                      self.inputAccessoryView = toolbar


                      @objc func doneClicked()
                      self.endEditing(true)




                      And call it like below:-



                      class DoneKeyboardViewController: UIViewController 

                      @IBOutlet weak var txtField: UITextField!

                      override func viewDidLoad()
                      super.viewDidLoad()

                      txtField.addDoneButton()







                      share|improve this answer

























                        2












                        2








                        2







                        you could simply create an extension of UITextField like below:-



                        extension UITextField 

                        func addDoneButton()
                        let toolbar = UIToolbar()
                        toolbar.sizeToFit()

                        let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneClicked))

                        toolbar.setItems([doneButton], animated: false)

                        self.inputAccessoryView = toolbar


                        @objc func doneClicked()
                        self.endEditing(true)




                        And call it like below:-



                        class DoneKeyboardViewController: UIViewController 

                        @IBOutlet weak var txtField: UITextField!

                        override func viewDidLoad()
                        super.viewDidLoad()

                        txtField.addDoneButton()







                        share|improve this answer













                        you could simply create an extension of UITextField like below:-



                        extension UITextField 

                        func addDoneButton()
                        let toolbar = UIToolbar()
                        toolbar.sizeToFit()

                        let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneClicked))

                        toolbar.setItems([doneButton], animated: false)

                        self.inputAccessoryView = toolbar


                        @objc func doneClicked()
                        self.endEditing(true)




                        And call it like below:-



                        class DoneKeyboardViewController: UIViewController 

                        @IBOutlet weak var txtField: UITextField!

                        override func viewDidLoad()
                        super.viewDidLoad()

                        txtField.addDoneButton()








                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Mar 8 at 1:50









                        Abhinav KhandujaAbhinav Khanduja

                        3119




                        3119























                            1














                            you can try this extension of UITextField.



                            the UITextField's extension is global, the TextFieldDone protocol is used for tagging where you add keyboards you choose.



                            class ViewController: UIViewController, TextFieldDone 
                            @IBOutlet weak var textField: UITextField!

                            override func viewDidLoad()
                            super.viewDidLoad()
                            textField.addDoneButtonOnKeyboard(self)


                            @objc
                            func doneButtonAction(_ textField: UITextField)
                            print("this")



                            protocol TextFieldDone
                            func doneButtonAction(_ textField: UITextField)


                            extension UITextField: TextFieldDone

                            func addDoneButtonOnKeyboard(_ target: UIViewController)

                            let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
                            doneToolbar.barStyle = UIBarStyle.blackTranslucent
                            let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
                            let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: target, action: #selector(doneButtonAction(_:)))

                            var items = [UIBarButtonItem]()
                            items.append(flexSpace)
                            items.append(done)
                            doneToolbar.items = items
                            doneToolbar.sizeToFit()
                            inputAccessoryView = doneToolbar


                            @objc func doneButtonAction(_ textField: UITextField)
                            print("PlaceHolder")







                            share|improve this answer





























                              1














                              you can try this extension of UITextField.



                              the UITextField's extension is global, the TextFieldDone protocol is used for tagging where you add keyboards you choose.



                              class ViewController: UIViewController, TextFieldDone 
                              @IBOutlet weak var textField: UITextField!

                              override func viewDidLoad()
                              super.viewDidLoad()
                              textField.addDoneButtonOnKeyboard(self)


                              @objc
                              func doneButtonAction(_ textField: UITextField)
                              print("this")



                              protocol TextFieldDone
                              func doneButtonAction(_ textField: UITextField)


                              extension UITextField: TextFieldDone

                              func addDoneButtonOnKeyboard(_ target: UIViewController)

                              let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
                              doneToolbar.barStyle = UIBarStyle.blackTranslucent
                              let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
                              let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: target, action: #selector(doneButtonAction(_:)))

                              var items = [UIBarButtonItem]()
                              items.append(flexSpace)
                              items.append(done)
                              doneToolbar.items = items
                              doneToolbar.sizeToFit()
                              inputAccessoryView = doneToolbar


                              @objc func doneButtonAction(_ textField: UITextField)
                              print("PlaceHolder")







                              share|improve this answer



























                                1












                                1








                                1







                                you can try this extension of UITextField.



                                the UITextField's extension is global, the TextFieldDone protocol is used for tagging where you add keyboards you choose.



                                class ViewController: UIViewController, TextFieldDone 
                                @IBOutlet weak var textField: UITextField!

                                override func viewDidLoad()
                                super.viewDidLoad()
                                textField.addDoneButtonOnKeyboard(self)


                                @objc
                                func doneButtonAction(_ textField: UITextField)
                                print("this")



                                protocol TextFieldDone
                                func doneButtonAction(_ textField: UITextField)


                                extension UITextField: TextFieldDone

                                func addDoneButtonOnKeyboard(_ target: UIViewController)

                                let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
                                doneToolbar.barStyle = UIBarStyle.blackTranslucent
                                let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
                                let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: target, action: #selector(doneButtonAction(_:)))

                                var items = [UIBarButtonItem]()
                                items.append(flexSpace)
                                items.append(done)
                                doneToolbar.items = items
                                doneToolbar.sizeToFit()
                                inputAccessoryView = doneToolbar


                                @objc func doneButtonAction(_ textField: UITextField)
                                print("PlaceHolder")







                                share|improve this answer















                                you can try this extension of UITextField.



                                the UITextField's extension is global, the TextFieldDone protocol is used for tagging where you add keyboards you choose.



                                class ViewController: UIViewController, TextFieldDone 
                                @IBOutlet weak var textField: UITextField!

                                override func viewDidLoad()
                                super.viewDidLoad()
                                textField.addDoneButtonOnKeyboard(self)


                                @objc
                                func doneButtonAction(_ textField: UITextField)
                                print("this")



                                protocol TextFieldDone
                                func doneButtonAction(_ textField: UITextField)


                                extension UITextField: TextFieldDone

                                func addDoneButtonOnKeyboard(_ target: UIViewController)

                                let doneToolbar: UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
                                doneToolbar.barStyle = UIBarStyle.blackTranslucent
                                let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
                                let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: target, action: #selector(doneButtonAction(_:)))

                                var items = [UIBarButtonItem]()
                                items.append(flexSpace)
                                items.append(done)
                                doneToolbar.items = items
                                doneToolbar.sizeToFit()
                                inputAccessoryView = doneToolbar


                                @objc func doneButtonAction(_ textField: UITextField)
                                print("PlaceHolder")








                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Mar 19 at 0:46

























                                answered Mar 8 at 1:21









                                dengAprodengApro

                                1,16911324




                                1,16911324





















                                    0














                                    You could create a new view controller class that implements this functionality, then use it as a super class for the rest of your view controllers






                                    share|improve this answer



























                                      0














                                      You could create a new view controller class that implements this functionality, then use it as a super class for the rest of your view controllers






                                      share|improve this answer

























                                        0












                                        0








                                        0







                                        You could create a new view controller class that implements this functionality, then use it as a super class for the rest of your view controllers






                                        share|improve this answer













                                        You could create a new view controller class that implements this functionality, then use it as a super class for the rest of your view controllers







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Mar 8 at 1:12









                                        Shannon DShannon D

                                        1




                                        1





















                                            0














                                            You could subclass UIViewController and override its viewDidLoad with the implementation you've presented and then set all of your view controller classes in interface builder to your derived class instead of UIViewController.



                                            Or you could subclass UITextField and set its keyboard to the Done keyboard as a default in its init method with:



                                            class TextField : UITextField 
                                            required init?(coder aDecoder: NSCoder)
                                            super.init(coder: aDecoder)
                                            self.returnKeyType = .done




                                            Then everywhere in your storyboards where you have a text field that you want to show the done button, you set the text field's class to TextField instead of UITextField. You need the initWithCoder because that's the initializer that gets called when creating the class from a storyboard.






                                            share|improve this answer



























                                              0














                                              You could subclass UIViewController and override its viewDidLoad with the implementation you've presented and then set all of your view controller classes in interface builder to your derived class instead of UIViewController.



                                              Or you could subclass UITextField and set its keyboard to the Done keyboard as a default in its init method with:



                                              class TextField : UITextField 
                                              required init?(coder aDecoder: NSCoder)
                                              super.init(coder: aDecoder)
                                              self.returnKeyType = .done




                                              Then everywhere in your storyboards where you have a text field that you want to show the done button, you set the text field's class to TextField instead of UITextField. You need the initWithCoder because that's the initializer that gets called when creating the class from a storyboard.






                                              share|improve this answer

























                                                0












                                                0








                                                0







                                                You could subclass UIViewController and override its viewDidLoad with the implementation you've presented and then set all of your view controller classes in interface builder to your derived class instead of UIViewController.



                                                Or you could subclass UITextField and set its keyboard to the Done keyboard as a default in its init method with:



                                                class TextField : UITextField 
                                                required init?(coder aDecoder: NSCoder)
                                                super.init(coder: aDecoder)
                                                self.returnKeyType = .done




                                                Then everywhere in your storyboards where you have a text field that you want to show the done button, you set the text field's class to TextField instead of UITextField. You need the initWithCoder because that's the initializer that gets called when creating the class from a storyboard.






                                                share|improve this answer













                                                You could subclass UIViewController and override its viewDidLoad with the implementation you've presented and then set all of your view controller classes in interface builder to your derived class instead of UIViewController.



                                                Or you could subclass UITextField and set its keyboard to the Done keyboard as a default in its init method with:



                                                class TextField : UITextField 
                                                required init?(coder aDecoder: NSCoder)
                                                super.init(coder: aDecoder)
                                                self.returnKeyType = .done




                                                Then everywhere in your storyboards where you have a text field that you want to show the done button, you set the text field's class to TextField instead of UITextField. You need the initWithCoder because that's the initializer that gets called when creating the class from a storyboard.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Mar 8 at 1:15









                                                Matt LongMatt Long

                                                22.7k46492




                                                22.7k46492





















                                                    0














                                                    Try this :



                                                    class CustomTextField: UITextField 
                                                    override init(frame: CGRect)
                                                    super.init(frame: frame)
                                                    addDoneButtonOnKeyboardToInput()

                                                    required init?(coder aDecoder: NSCoder)
                                                    super.init(coder: aDecoder)
                                                    addDoneButtonOnKeyboardToInput()

                                                    var doneToolbar: UIToolbar?
                                                    var doneButton: UIBarButtonItem?
                                                    func addDoneButtonOnKeyboardToInput()
                                                    if (doneToolbar == nil)
                                                    doneToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
                                                    doneToolbar?.barStyle = UIBarStyle.default

                                                    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
                                                    doneButton = UIBarButtonItem(title: SF.buttons.done, style: UIBarButtonItem.Style.done, target: nil, action: #selector(doneButtonAction(sender:)))

                                                    var items = [UIBarButtonItem]()
                                                    items.append(flexSpace)
                                                    if let btn = doneButton
                                                    items.append(btn)


                                                    doneToolbar?.items = items

                                                    doneButton?.target = self
                                                    doneToolbar?.sizeToFit()

                                                    self.inputAccessoryView = doneToolbar



                                                    @objc
                                                    func doneButtonAction(sender: UIBarButtonItem)
                                                    self.resignFirstResponder()







                                                    share|improve this answer



























                                                      0














                                                      Try this :



                                                      class CustomTextField: UITextField 
                                                      override init(frame: CGRect)
                                                      super.init(frame: frame)
                                                      addDoneButtonOnKeyboardToInput()

                                                      required init?(coder aDecoder: NSCoder)
                                                      super.init(coder: aDecoder)
                                                      addDoneButtonOnKeyboardToInput()

                                                      var doneToolbar: UIToolbar?
                                                      var doneButton: UIBarButtonItem?
                                                      func addDoneButtonOnKeyboardToInput()
                                                      if (doneToolbar == nil)
                                                      doneToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
                                                      doneToolbar?.barStyle = UIBarStyle.default

                                                      let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
                                                      doneButton = UIBarButtonItem(title: SF.buttons.done, style: UIBarButtonItem.Style.done, target: nil, action: #selector(doneButtonAction(sender:)))

                                                      var items = [UIBarButtonItem]()
                                                      items.append(flexSpace)
                                                      if let btn = doneButton
                                                      items.append(btn)


                                                      doneToolbar?.items = items

                                                      doneButton?.target = self
                                                      doneToolbar?.sizeToFit()

                                                      self.inputAccessoryView = doneToolbar



                                                      @objc
                                                      func doneButtonAction(sender: UIBarButtonItem)
                                                      self.resignFirstResponder()







                                                      share|improve this answer

























                                                        0












                                                        0








                                                        0







                                                        Try this :



                                                        class CustomTextField: UITextField 
                                                        override init(frame: CGRect)
                                                        super.init(frame: frame)
                                                        addDoneButtonOnKeyboardToInput()

                                                        required init?(coder aDecoder: NSCoder)
                                                        super.init(coder: aDecoder)
                                                        addDoneButtonOnKeyboardToInput()

                                                        var doneToolbar: UIToolbar?
                                                        var doneButton: UIBarButtonItem?
                                                        func addDoneButtonOnKeyboardToInput()
                                                        if (doneToolbar == nil)
                                                        doneToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
                                                        doneToolbar?.barStyle = UIBarStyle.default

                                                        let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
                                                        doneButton = UIBarButtonItem(title: SF.buttons.done, style: UIBarButtonItem.Style.done, target: nil, action: #selector(doneButtonAction(sender:)))

                                                        var items = [UIBarButtonItem]()
                                                        items.append(flexSpace)
                                                        if let btn = doneButton
                                                        items.append(btn)


                                                        doneToolbar?.items = items

                                                        doneButton?.target = self
                                                        doneToolbar?.sizeToFit()

                                                        self.inputAccessoryView = doneToolbar



                                                        @objc
                                                        func doneButtonAction(sender: UIBarButtonItem)
                                                        self.resignFirstResponder()







                                                        share|improve this answer













                                                        Try this :



                                                        class CustomTextField: UITextField 
                                                        override init(frame: CGRect)
                                                        super.init(frame: frame)
                                                        addDoneButtonOnKeyboardToInput()

                                                        required init?(coder aDecoder: NSCoder)
                                                        super.init(coder: aDecoder)
                                                        addDoneButtonOnKeyboardToInput()

                                                        var doneToolbar: UIToolbar?
                                                        var doneButton: UIBarButtonItem?
                                                        func addDoneButtonOnKeyboardToInput()
                                                        if (doneToolbar == nil)
                                                        doneToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 50))
                                                        doneToolbar?.barStyle = UIBarStyle.default

                                                        let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
                                                        doneButton = UIBarButtonItem(title: SF.buttons.done, style: UIBarButtonItem.Style.done, target: nil, action: #selector(doneButtonAction(sender:)))

                                                        var items = [UIBarButtonItem]()
                                                        items.append(flexSpace)
                                                        if let btn = doneButton
                                                        items.append(btn)


                                                        doneToolbar?.items = items

                                                        doneButton?.target = self
                                                        doneToolbar?.sizeToFit()

                                                        self.inputAccessoryView = doneToolbar



                                                        @objc
                                                        func doneButtonAction(sender: UIBarButtonItem)
                                                        self.resignFirstResponder()








                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Mar 8 at 1:56









                                                        Amir.n3tAmir.n3t

                                                        173110




                                                        173110



























                                                            draft saved

                                                            draft discarded
















































                                                            Thanks for contributing an answer to Stack Overflow!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid


                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.

                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function ()
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55054907%2fadd-keyboard-done-button-using-global-function%23new-answer', 'question_page');

                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            1928 у кіно

                                                            Захаров Федір Захарович

                                                            Ель Греко