Wednesday, 19 November 2014

Swift + Add UIToolbar or Done Button on Keyboard

   

  override func viewDidLoad()
    {
        super.viewDidLoad()
     
        //--- add UIToolBar on keyboard and Done button on UIToolBar ---//
        self.addDoneButtonOnKeyboard()
        

    }





    //--- *** ---//
    
    func addDoneButtonOnKeyboard()
    {
        var doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 50))
        doneToolbar.barStyle = UIBarStyle.BlackTranslucent
       
        var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
        var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("doneButtonAction"))
        
        var items = NSMutableArray()
        items.addObject(flexSpace)
        items.addObject(done)
      
        doneToolbar.items = items
        doneToolbar.sizeToFit()
        
        self.textView.inputAccessoryView = doneToolbar
        self.textField.inputAccessoryView = doneToolbar
        
    }
    
    func doneButtonAction()
    {
        self.textViewDescription.resignFirstResponder()
        self.textViewDescription.resignFirstResponder()
    }

14 comments:

  1. //a slightly more generalized solution based on above
    func addDoneButtonOnKeyboard(view: UIView?)
    {

    var doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 50))
    doneToolbar.barStyle = UIBarStyle.BlackTranslucent
    var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: view, action: "resignFirstResponder")
    var items = [AnyObject]()
    items.append(flexSpace)
    items.append(done)

    doneToolbar.items = items
    doneToolbar.sizeToFit()
    if let accessorizedView = view as? UITextView {
    accessorizedView.inputAccessoryView = doneToolbar
    accessorizedView.inputAccessoryView = doneToolbar
    } else if let accessorizedView = view as? UITextField {
    accessorizedView.inputAccessoryView = doneToolbar
    accessorizedView.inputAccessoryView = doneToolbar
    }

    }

    ReplyDelete
    Replies
    1. Hi Dude,
      How do you add two buttons where one button on left and another one on right ???

      Delete
    2. var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)

      this is use to cover space left side

      Delete
    3. JLB, can you clarify how and where to call this from and what to pass as UIView?

      Delete
  2. Naveen, can you clarify what textView is referring to? What do you actually link this to?

    ReplyDelete
    Replies
    1. The name of your UITextView or UITextField that opens the keyboard when it is touched.

      Delete
  3. `doneToolbar.items = items`gives the error: can not assign value of type NSMutableArray to a value of type [UIBarButtonItem]

    Any ideas hot to solve? Thanks!

    ReplyDelete
    Replies
    1. that is because of xcode.....directly access the array like this

      doneToolbar.items = [done]

      Delete
  4. Instead of creating the NSMutableArray and add that, try instead:
    doneToolbar.items?.append(flexSpace)
    doneToolbar.items?.append(done)

    That removed the error message, but I can't get the button to show. Please tell if you work it out.

    ReplyDelete
    Replies
    1. This works:

      var items = [AnyObject]()
      items.append(flexSpace)
      items.append(done)
      doneToolbar.items = items as! [UIBarButtonItem]

      Delete
    2. Brilliant! That's a useful logic for many "type problems" Thanks!

      Delete
  5. var items = [UIBarButtonItem]() is exact way to do this, rather than unwrapping

    ReplyDelete
  6. small code improvements:

    let doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, bounds.size.width, 50))
    doneToolbar.barStyle = .Default

    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    let done: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: Selector("doneButtonAction"))

    var items: [UIBarButtonItem] = []
    items.append(flexSpace)
    items.append(done)

    doneToolbar.items = items
    doneToolbar.sizeToFit()

    ReplyDelete