Tuesday 2 August 2016

Add button to the UIToolbar - iOS Swift

This code will really helpful to add button to the UIToolbar present in keyboard or pickerview.

For example:
If you want "Done" button to be inserted in the toolbar of the pickerview. you just have to follow simple steps as follows.

Step 1:
Have to include this code inside "viewDidLoad" to create "Done" button in UIToolbar. "textBoxText" is the name of the Text field.

      
        // create done button in toolbar.
        doneToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
        doneToolbar.barStyle = UIBarStyle.Default
        doneToolbar.items =   [UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(FirstViewController.PickerDoneButtonTapped))]
        doneToolbar.sizeToFit()
        textBoxText.inputAccessoryView = doneToolbar



Step 2:
Code the function of "Done" button that has included in the UIToolbar. I have given that if  "Done" button is tapped the PickerView have to disable.

    func PickerDoneButtonTapped()
    {
        textBoxText.resignFirstResponder()
    }

Step 3:
Have to call the function in "viewDidload"

self.PickerDoneButtonTapped()

Output:

                                                                                 
      

No comments: