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()
}
//a slightly more generalized solution based on above
ReplyDeletefunc 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
}
}
Hi Dude,
DeleteHow do you add two buttons where one button on left and another one on right ???
var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
Deletethis is use to cover space left side
JLB, can you clarify how and where to call this from and what to pass as UIView?
DeleteNaveen, can you clarify what textView is referring to? What do you actually link this to?
ReplyDeleteThe name of your UITextView or UITextField that opens the keyboard when it is touched.
Delete`doneToolbar.items = items`gives the error: can not assign value of type NSMutableArray to a value of type [UIBarButtonItem]
ReplyDeleteAny ideas hot to solve? Thanks!
that is because of xcode.....directly access the array like this
DeletedoneToolbar.items = [done]
Instead of creating the NSMutableArray and add that, try instead:
ReplyDeletedoneToolbar.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.
This works:
Deletevar items = [AnyObject]()
items.append(flexSpace)
items.append(done)
doneToolbar.items = items as! [UIBarButtonItem]
Brilliant! That's a useful logic for many "type problems" Thanks!
Deletevar items = [UIBarButtonItem]() is exact way to do this, rather than unwrapping
ReplyDeletesmall code improvements:
ReplyDeletelet 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()
what is bounds.size.width
ReplyDelete