Is there a way to programmatically enable or disable the Return Key on the UIKeyboard
? The closest I could find is enablesReturnKeyAutomatically
, but that only will tell whether to disable it at all.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
One good idea is to create one file to access this class from anywhere. Here is the code:
UIKeyboard.h
UIKeyboard.m
Now you can import and access this class from your own class:
A full documentation of this class you can find here: http://ericasadun.com/iPhoneDocs/_u_i_keyboard_8h-source.html
More information you can find here: http://cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html
If you can get the UIKeyboard object itself (something not exposed in the SDK, mind you, so Apple may not be happy if you use these calls), then there's a convenient setReturnKeyEnabled: member function.
(via Erica Sadun's dump of the 2.2 iPhone frameworks)
The implementation of
magicallyGetAUIKeyboardInstance
is described here.May be following code segment helps.
This is publically available in iPhone SDK in UITextInputTraits. Using this, return key will be disabled when no input text is available within text field
UITextField
'senablesReturnKeyAutomatically
property can be set right in Interface Builder, just select the textfield and open the Attributes inspector. As Tharindu stated, this will automatically enable and disable the return key depending on whether any text has been entered.Of course, if you need to change this in code you can still set it programmatically using
nameTextField.enablesReturnKeyAutomatically = true
.EDIT to address the downvotes:
Otherwise, there is no official way to enable and disable the return key on command. I would recommend against trying to use private APIs to accomplish this. Alternatively, you can use the
textFieldShouldReturn:
delegate method and put your conditional/validation there and respond accordingly.Here is a technique that is available from the documented API, but it does not provide visual feedback when the enter key is disabled.
Other answers here can be used with
to provide dynamic visual feedback as the user types.
You can override
UITextField
'shasText
attribute to achieve this:Where
evaluateString(_ text: String?) -> Bool
checks against your needed input criteria, for example character count.Of course this does only work in combination with
enablesReturnKeyAutomatically = true
set on theUITextField
.I am aware that my answer is neither timely nor written in Objective-C, but given that I have not been able to find an answer anywhere else and this question being routinely referred to in other threads, I think that here is the best place to post it.