iOS UISearchBar's keyboard partly hides tableView - ios

I have an UIView (implementing UISearchBarDelegate), on which I have UIImageView, UITableView and UISearchbar.
When searching, the tableView with search results gets partly hidden by keyboard. How could I make the tableView to scroll when keyboard appears?
Maybe I should just make the view frame smaller for the time the search is being implemented. On the other hand, as I've read, this approach is not recommended.

You should register your view to be an observer of the UIKeyboard notifications. When the keyboard changes its state, a notification is sent and from the notification object you can retrieve the keyboard frame, which allows you to resize your table view frame accordingly. As soon as the keyboard disappears, you will get a new notification and then you will be able to resize the table view again. Besides these notifications (read the developer library doc for details) allow you to synchronized perfectly your view resizing with the keyboard animations.
Note that you can also use the system defined UISearchDisplayController object which takes care of these details (and other) for you. It's a strange beast (it's not a UIViewController subclass for instance) but if you learn how to use it will save you a lot of extra work.

Related

Custom Keyboard Accessory view input

I have created a custom accessory view to supplement the standard Apple alpha iOS keyboard.
The purpose is to add a line of numeric keys to prevent flipping back and forth between keyboard views. At first, I created a toolbar and loaded it with a set of 0 - 9 titled buttonItems and it functioned quite well. However, it looked terrible, not at all like the alpha keys despite adding a rounded rect background image to each key because the system apparently prevents customizing font size and button spacing inside the stack view of the toolbar. Therefore, I created a UIView xib and loaded it with a stackView full of customized numerical buttons. When I add the UIView as the accessory view it looks pretty darn close to the rest of the Apple Alpha keyboard. The issue now is that the touch-up events go to the UIView class of the accessory view. Is there a clever, efficient way to have the button presses in the accessory emulate the std keyboard feeding into TextField: shouldChangeCharactersIn? I could package the button presses into a local notification event to get it into the class holding the textField but that seems terribly inelegant! Any suggestions would be greatly appreciated. Stay Safe!
Not the best answer, but I did implement notification on key button press with an observer in the main view class. The observer does a TextField.insertText which is suboptimal since I will need to refactor the several hundred lines of code that performs real-time language translation in the shouldChangeCharacters methods. Ah well.

UITableView: Can't find a way to move/align the tableIndex element up

I have a UITableView and I am using a tableIndex, my problem is that when the keyboard comes up, it partially hides the tableIndex.
An acceptable solution would be to move the tableIndex up (like what the Contacts App does) when the keyboard appears, and show the most of it, but can figure out a way to do this. Any ideas of code snippet I can try?
The way to do this is to register to Keyboard Will Show notification and then update the height of your controller's view. You could also embed everything in a scroll View so that once the keyboard rises, your view becomes smaller and scrollable.
It is honestly a bit annoying to set up, so, if you are open to using CocoaPods then IQKeyboardManager is probably your best bet which can be found here: https://github.com/hackiftekhar/IQKeyboardManager

Adding Accessibility to piano style UI element

I'm trying to make a musical keyboard UI element accessible. Just like how GarageBand does it. In other words, at first touch user is told by VoiceOver that they are touching a musical keyboard, and from that point every tap on musical keyboard view plays notes and there’s no further VoiceOver interruptions until user touches outside of the musical keyboard frame.
I have a UICollectionView where each cell represents a musical key and when user taps on it notes are played as expected. However, I have trouble getting this to work like the GB does. For the UICollectionView object, I’ve set accessibilityLabel and have set accessibilityTraits to UIAccessibilityTraitAllowsDirectInteraction. But that doesn’t seem to work. It doesn’t play any notes when VoiceOver is on. On the first tap VoiceOver anounces whatever the accessibilityLabel is set to and then just beeps on every tap.
I have custom UIGestureRecogniser subclass that I use for the collection view cell tap detection. Do I need to do something special under these circumstances?
Any ideas? Do I need to be doing anything else?
Figured it out. Not sure what the reasoning is however.
Embeded the musical keyboard UICollectionView in another view and made that view accessible with the UIAccessibilityTraitAllowsDirectInteractiontrait. Now it works as expected.

Swap out a custom inputView for the standard keyboard in iOS

I have a custom inputView for a particular textfield, and it works well. However, I cannot discern how to dismiss the view and get the regular keyboard back. (I have a SWAP button right next to the TextField.) I tried setting the textfield's inputView to nil, but that did nothing.
I do not need a full custom keyboard, but I need more than an Accessory view above the keyboard, which is why I am trying this route. I need about 20 custom buttons in addition to the regular keyboard, and I do not like the idea of a huge Accessory view taking up so much space.
I also would rather not require the user to initially install a full custom keyboard before being able to use the app.
Thank you very much for any suggestions.
I think you will probably have to do this:
Call resignFirstResponder on the UITextField
After the animation finishes, set your inputView to nil
Call becomeFirstResponder on the text field
The keyboard animation duration is sent in the userInfo dictionary on the keyboard presentation notifications.
In addition to the accepted answer, you can use reloadInputViews() (and this is less likely to suffer any animation glitches resulting from the resignFirstResponder, becomeFirstResponder calls):
yourTextField.inputView = nil;
yourTextField.reloadInputViews();
Here's more info in the Apple's Docs.

Animating UITextInput's textInputView

UIKit text input components, such as UITextView and UITextField have a property inputView to add a custom keyboard. There are two questions I have relating to this.
If the keyboard is currently visible and the property is set to a new input view, nothing happens. Resigning and regaining first responder status refreshes the input and displays the new view. Is this the best way to do it? If so it might answer my bigger question:
Is it possible to animate the transition between two input views?
From the UIResponder docs:
Responder objects that require a custom view to gather input from the user should redeclare this property as readwrite and use it to manage their custom input view. When the receiver subsequently becomes the first responder, the responder infrastructure presents the specified input view automatically. Similarly, when the view resigns its first responder status, the responder infrastructure automatically dismisses the specified view.
So unfortunately the answer to 1 is Yes and 2 is No.
Actually there is a method to do it cleanly: UIResponder's reloadInputViews, available from iOS 3.2!
I think you can animated it with some extra work:
Create a clear background window of a higher UIWindowLevel than the keyboard window.
Add your custom keyboard there and animate its frame into place.
Then set it as your text input's inputView and refresh the first responder as you do.
Your custom keyboard will change its parent view from your custom window to the keyboard one, but hopefully the user won't notice ;)

Resources