I want to add a cancel button to hide the keyboard if the user wants to. I cant find any solution for swift 3.
Take a look at the documentation of a UITextField's inputAccessoryView: https://developer.apple.com/reference/uikit/uitextfield/1619627-inputaccessoryview
Assigning a view to this property causes that view to be displayed above the standard system keyboard (or above the custom input view if one is provided) when the text field becomes the first responder. For example, you could use this property to attach a custom toolbar to the keyboard.
I recommend to assign a UIToolbar that contains your cancel button to the inputAccessoryView of the UITextField that needs the cancel button. Then hook up an action to the button that resigns the first responder from the UITextField.
Related
I want to always display keyboard in all views in pageviewcontroller.
All views have uitextfield and I coded becomefirstresponder in viewdidload so the keyboard appears.
But problem is that when i set new viewcontroller or swipe in pageviewcontroller the keyboard disappears and re-appears.
Can I always show keyboard?
Thanks.
Go simple.. set invisible textfield (means hidden one)! Set that as a first responder basically, this text field should be in base view controller means out of page view. as soon as you swipe the page shift responder to the required text field. hence it will not disappear and appear on page shift.
UITextField resigns as first responder on any tap/swipe gesture. You need to handle it in your scrollview delegate method when you detect the scroll ended and then set the current page view controller's UITextField as first responder.
I am wondering what the best approach for dismissing a keyboard is when using a UITextView. At the minute for UITextFields I dismiss the keyboard when the return button is pressed. However for the TextView I want to have the return button actually add new lines so there is no additional buttons remaining to close.
What if any is the current "standard" approach for dealing with this issue? For example is it to add an additional button on the screen or is there another approach?
Thanks
On the iPad there is a "dismiss" button on the keyboard. For iPhones, add an inputAccessoryView to the UITextView. Put a "dismiss" button there.
If you have several fields (text fields, text views, etc.) in some sort of form, you could have a standard inputAccessoryView for all of them. Include a "Next" button (to go to the next field) and a "Done" button (to dismiss the keyboard).
I need to show UIKeyboard with done button on top right corner ..please look into attached image..any help would be appreciated.
This might help you. please check it out BSKeyboardControls
Create a UIToolbar keep two bar buttons in that. Initially Hide that toolbar.
Show the toolbar on the textfield didBeginEditing delegate of textfield
and hide the toolbar in didEndEditing delegate of textfield
EDIT: As prashant said BSKeyboard offers what i said. See the look and feel of it here http://www.cocoacontrols.com/platforms/ios/controls/bskeyboardcontrols
You should set inputAccessoryView propery of UITextField or UITextView to your custom view with all required buttons
#property(readwrite, retain) UIView *inputAccessoryView
Description
The custom accessory view to display when the text field becomes the
first responder The default value of this property is nil. Assigning a
view to this property causes that view to be displayed above the
standard system keyboard (or above the custom input view if one is
provided) when the text field becomes the first responder. For
example, you could use this property to attach a custom toolbar to the
keyboard.
This has been bothering me for few weeks.
Here's a screenshot:
I copied this view Controller from a sample project.
Now I need to replace the Textfield with a UITextView instead.
The problem is I can't seem to be able to drag and drop it to a Bar Button Item.
I literally removed the textField then tried to drag and drop the textview, the Bar Button Item just wont highlight.
What am I missing?
PS: it works with UITextfield only.. why ?? o.O'
I think you are referring to the inputAccessoryView, it's a UIView which gets displayed when the textview becomes first responder. hence when the keyboard shows.
You can customize the UIView to hold all controls you want: textview, buttons, etc. More details here.
Basically, you implement your custom keyboard tool bar which is a UIView subclass (I assume you want something like the messages app), then you set the custom UIView to the inputAccessoryView property of the UITextView. here is a relevant link to get you started: http://gabriel-tips.blogspot.com/2011/05/input-accessory-view-how-to-add-extra.html
In my app, I have a textfield that takes its input from a picker view. However, the user could not find his preferred option so I added above the textfield a small button resembling a keyboard. Once the button is pressed the textfield's picker view should disappear and the keyboard should appear instead so that the user enter his preferred data. So how can I do that? I tried resigning it as first responder and then making it become the first responder hoping the picker could be released as the input view but it didn't work. Adding a textfield as an accessory input view will not look right (having two textfield the user should update the first one instead).