Keep textfield above keyboard - objective-c

I am trying to figure out how to keep the textfield about the keyboard in IOS. I tried the different code on stackoverflow but none works perfectly. Like if the textfield is above the keyboard and I click on the field, it does not move up. How can I show the textfield above the keyboard at all times? thanks!

lakesh's link is an excellent resource to look at.
There are essentially two ways to handle the situation.
Method 1:
You can encapsulate your view inside a UIScrollView and when the keyboard pops up, you should scroll your entire view up an equal distance to account for the space taken up by the keyboard.
Method 2:
Take the ultimate parent UIView inside your current UIViewController and change its frame (ideally with an animation) so that it moves off the top of the screen and makes way for the keyboard.
As a general guide, Method 1 is the preferred method. This is because you can still access the UI 'higher up' in the UIScrollView by scrolling up to it (consider say, a form with multiple fields). In Method 2 the user cannot return to the other elements in the view without the keyboard first being dismissed. Of course, it may be that you don't need to see the rest of the view while accepting keyboard input, but that decision is up to you.

Related

How do I properly move a scrollview when assigning a first responder?

I have a form in a UIView which consist of multiple textfields, couple of textviews and two fields that are interacted with by the means of an invisible button overlaid on them. When you click on a textfield, the keyboard pops up for text entry and I added a toolbar on it for navigating to the previous and next data entry (whatever the data entry is, be it a textfield, textview or one of the two special cases that are interacted with a button). Now, when I navigate with between textfields with those buttons everything works fine. My scrollview's content moves along the element that becomes the firstresponder (with the help of a piece of code from stackoverflow that scrolls the view while taking into account the height of the keyboard that hides a good portion of it). Here is a visual example.
The problem arises when I want to switch out of a specific data entry (date) that interacts with a hidden button. I'll give some context first. Those data entries show datepickers (one for the date, another for the time) in action sheets, and those action sheets also have navigating button in a toolbar, like so.
The code from stackoverflow that readjust the view do so in the didBeginEditing delegate methods of the textfields and textviews, so when I assign them first responder the scrollview adjust itself while taking into account the keyboard.
This means that, in the case that I switch into a special data entry, I manually move the scrollview's content so I can view the next element. When I switch from a special entry into a textfield I assume that the previously mentioned code from stackoverflow kicks in and adjust the view. It actually does when I move from the hour data entry (which uses a datepicker in an actionsheet) into the next element which is a textfield. However, when I'm at the date data entry (which is directly before the hour data entry) and press previous to assign the first responder to the textfield above it, the scrollview goes way ahead the text field, like so.
What's important to note is that this problem only occurs when the textfield is not in view. This makes me suspect that I'm incorrectly using the code that readjust the view. For instance, there is a constant in the code that represents the height of the keyboard. I tried changing it from 216 to 260 (so it takes into account my toolbar added on top of the keyboard) but this results in strange black artifacts near the buttom that only occasionally appears.
I'm kind of lost in this bug, and my post is already pretty long. I've prepared an example of my problem in a new project, if any of you could take a look into it I'd be very appreciative.
Here it is

Controlling NSSegmentedControl with the keyboard

I have a form in my Cocoa app that contains an NSSegmentedControl that I want to be controllable via the keyboard. It seems that NSSegmentedControl is very reluctant to become the first responder, however.
Setting the initial first responder of the window to the segmented control does nothing -- it will not have keyboard focus when the window is first loaded. It does receive focus if I manually set the first responder like this, however:
[segmentedControl.window makeFirstResponder: segmentedControl];
That will work fine if the only part of the form is the segmented control. If I add another field (say, an NSTextField), and I set the nextResponder of the segmented control to that field, the segmented control will never become first responder. Focus will immediately go to the text field, and pressing tab to switch back to the segmented control doesn't work.
I've tried subclassing NSSegmentedControl and overriding acceptsFirstResponder, becomeFirstResponder, etc. to no avail. The only one that makes any difference is resignFirstResponder -- if I return NO from that method then the segmented control will indeed retain focus, but obviously I don't want it to retain focus all the time.
Any ideas on how to get the control to behave like a normal responder?
It's behaving as intended. Not all controls participate in the "key view loop". Full keyboard navigation is turned on through Universal Access in System Preferences for all apps and it's not for individual apps to implement on their own.
It's best not to use a segmented control in a form intended for heavy keyboard entry. NSPopUpButton works more closely to what we all exepect in a web form so it's not as if it's necessarily the wrong choice in your app's UI.
Rather than answer exactly the question you asked (which someone else can do), I humbly suggest you choose on the side of functionality at the cost of a slightly prettier UI element since that prettier UI element wasn't intended to get along with the keyboard.

Block ui when keyboard is up

I've seen a couple of apps that show a transparent view on top of the current ui while the keyboard is present and if clicked it hides the keyboard. I looked around the web and couldnt find a solution for this problem.
Simply add a UIButton, custom type, the size of your screen and add it to your view when your text field (or other entry) takes focus. Make sure your edit view is brought to the front of its superview at the point you add the button (to ensure the edit view still responds to touch).
Add a target to the button which dismisses the keyboard ([myTextfield resignFirstResponder]) and removes the button.
Also make sure to remove the button when the textField dismisses normally.

iPad UIButtons responding to external keyboard input

I'm writing an app based around a calculator-like UI. On an iPad, I'd like the number UIButtons on my calculator UI to respond to the numbers on the external keyboard. I have no text input anywhere in the app, and I'd never want an on-screen keyboard to appear. How do I get my UIButtons to respond to specific key presses as if they'd been touched?
I'd say create a hidden UITextField with the delegate set, make it become the first responder upon viewDidLoad. The keyboard won't appear when a bluetooth keyboard is connected as far as I know, then just check what characters are typed in the shouldChangecharactersInRange and activate the right buttons accordingly.
EDIT: a quick example I just made, https://github.com/benjaminq42/buttonTest , if you wish to have a highlight animation of some sorts you need to create a custom UIButton class. You can use 'setHighlighted' but that 'sticks'.

adding an invisible button to the background in IB

I'm working with Xcode doing a Ipad app.
i simply want user to click anywhere on screen (not counting text fields) to perform some IBAction.I'm using an invisible button that covers my whole view.
Since I have some text fields in my view,i need to add this invisible button to the background of my user interface. I cant seem to find this option in the button attributes? any help?
Just set the button's type to custom.
Did you try setting the opacity of the button to zero?
I guess i got your point. You just want to put the UIButton(invisible) on the back of all the UITextField. The simple solution to this is open the Document Window in the IB. Now expand the view tree in the list view. Just drag your UIButton above the UITextFields and set the alpha value for the button in the property to be zero.
Hope this helps!!
iPad users don't "click". They "tap" or "touch".
In Interface Builder, I believe views are constructed with a z-index from top to bottom as they appear in the document window, so dragging your button so that it appears as the first subview of your main view should be a quick fix for this.
Have you considered other approaches? This doesn't sound like standard behaviour for an app and will probably cause havoc with anybody using Voice Over. What are you trying to accomplish?