Prevent default keyboard from showing when UITextField is pressed - iphone

Is there a way to make a custom keyboard to pop up when a user presses on a UITextField. I have a view with a custom keypad on it and I want that to come up when the user presses on the UITextField instead of the default apple keyboard.

Since iOS 3.2 there's a property for exactly that, called inputView.
just go like this: [myTextField setInputView:myInputView] - where myInputView is obviously your custom input view. Then the system will pop up your view instead of the predefined keyboards.

You may set delegate in your UITextField and return NO in textFieldShouldBeginEditing:.

not sure I understand the question? the keyboard type can be assigned to the UITextField so If you want a different one then specify it when initializing the object
UITextField * textFld = [[UITextField alloc] init];
textFld.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html#//apple_ref/doc/c_ref/UIKeyboardType

Related

Unable to Generate UITextField dynamically in place of UILabel?

I am trying to generate UITextField dynamically in place of UILabel.
Now i want to update that data.
I am displaying data in the UILabel from the database and there is an UIButton for editing.
When i click on that UIButton UITextField should be generated in place of UILabel and also data should be displayed in UITextField.
What you can do is to design a view with all textfield which works in two modes, first readonly (by setting userInteraction to false ) and second editing mode. This way you can avoid the use of labels. This will need only one edit button for all of the fields. if you still want to stick with your approach, you can hide the labels, use their frames to create textfields at their place and make them visible as long as you are working in edit mode. Don't forget to use
[self.view bringSubviewToFront:TEXT_FIELD];
While you add them to your view.
e
Managing the editing with the approach I mentioned earlier is mor easy and require less efforts. Hope it helps
you need to implement this textField in .h file, to get access to it when you finish aditing. Then, in your button callback:
textField = [[UITextField alloc] initWithFrame:[yourUILabel frame]];
[textField setText:yourUILabel.text];
[self.view addSubView:textField];
then, to replace it back:
[yourUILabel setText:textField.text];
[textField removeFromSuperView];
You can use the methods that others have described here but it sounds like you just want a UITextField that looks like a label and you want to control whether or not it's editable.
set enabled to YES / NO depending on whether you want the user to edit the UITextField
set the borderStyle of a UITextField (usually between UITextBorderStyleRoundedRect and UITextBorderStyleNone)
You can then toggle the enabled and borderStyle values as follows:
- on initial view: enabled: NO, border style: UITextBorderStyleNone
- on button tap: enabled: YES, border style: UITextBorderStyleRoundedRect
You don't have to mess with the view hierarchy and worry about frames, etc, this way.

Setting a UITextField into editing mode programmatically

I have a UITextField that I want to set into editing mode (keyboard on screen and cursor in text field box) programatically. I know that the user will be in editing mode when this view appears onscreen, so I want to save the user from having to tap the text field.
The "editing" property of a UITextField is read only - so that doesn't work. Is there a way to set the UITextField into editing mode, with a keyboard onscreen, programmatically?
Call becomeFirstResponder on the UITextField.
Related question:
How do I show the keyboard by default in UITextView?
You have to call [textField becomeFirstResponder];
Indeed call [textField becomeFirstResponder] in Obj-C or textField.becomeFirstResponder() in Swift.
However, make sure you call this in the viewDidAppear and not in the viewDidLoad to prevent strange behaviour (see: When set UITextField as FirstResponder programmatically, cause some weird actions on text editing).

How can I show a UIDatePicker instead of a keyboard when a user selects a UITextField?

I have a nice clean UI within a table view which has a few text fields for the user to fill out. One of the fields is for the user's birthday.
I'd like to have it so that when the user selects the birthday field, a view containing a UIDatePicker would come up as, just like the different keyboards do when selecting a text field.
Can this be done? I would have to prevent the text field from being the first responder (to avoid having the keyboard come up) and I would have to animate the view sliding up if no keyboard was showing before.
Would presenting the view modally be an option? If so how would I go about doing it? From the documentation it seems that modal views still take up the whole screen, I just want to use the lower 216 pixels (height of the keyboard and UIDatePicker).
Any one have any tips on how to go about doing this?
Old question but the correct way to do this these days would be to set the UITextField's inputView to a picker you created somewhere. Something like this:
UIPickerView *myPicker = [[UIPickerView alloc] init];
// set picker frame, options, etc...
// N.B. origin for the picker's frame should be 0,0
[myTextField setInputView:myPicker];
When you go to edit a UITextField, iOS really just displays whatever view is at textField.inputView which by default is the keyboard, you can make it anything you want as long as it's a subclass of UIView.
Regarding animation, take a look at DateCell sample application -
http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html
And in any case, the proper way to do this is set UITextField's inputView to show the picker instead of the keyboard. That's what it's meant to do. More on that here:
How can I present a picker view just like the keyboard does?
Cheers,
Oded.
I would implement this by just animating a view containing the UIDatePicker, a Done, and Cancel button) up from the bottom of the screen. Using CoreAnimation, this should be pretty easy.
Why are you using a text field if you don't want to accept user input from a keyboard? Instead use a UILabel subclass (where you override the touchesBegan/Ended:withEvent: set of methods to show the UIDatePicker) or a UIButton (where your action is a method which slides up the UIDatePicker).

Cocoa Touch - UISearchBar Keyboard - Hide 'Search' Button

On my iPhone app I'm using a UISearchBar (without a Search Display Controller). I have my own 'Search' button as a UIButton and therefore on the keyboard that pops up when editing begins on a UISearchBar I would like to constantly hide the 'Search' button on it as it is unusable. How could I go about doing this without the Search Display Controller?
Or instead - is it possible to continue using my UISearchBar and UIButton 'Search', and also get the keyboard 'Search' button to work as well? I have tried -
(void)searchBarSearchButtonClicked:(UISearchBar
*)searchBar
However my UISearchBar is called 'searchbar' and therefore isn't responding to that (changing the searchBar to searchbar doesn't work either). When I instead use a UISearchBar with a Search Display Controller is works perfectly - however, I don't want the new table view that it opens on editing - or is there a way to prevent this too?
Thanks in advance!
Benji
--EDIT: SOLVED. I just used a textfield instead upon the searchbar so it looks similar.
SOLVED. I just used a textfield instead upon the searchbar so it looks similar.
If you want to change UISearchBar's textfield, you may use the following code:
UITextField *textField = [searchBar.subviews objectAtIndex:1];
textField.returnKeyType = UIKeyboardTypeDefault;
textField.leftView = nil;
textField.delegate = self;
But if you want to have an edit bar, a customized one is more appropriate.
For example, create a toolbar and add a UITextField, a Flexible Space and a UIBarButtonItem with text "send" on it. Then it will look like the edit bar in SMS app.

UITextField focus

How can I programmatically assign focus to a specific UITextField in a view? I have several fields and on view display, I'd like to put the cursor on a specific field, not the first one at top.
Try doing this in viewWillAppear:
[desiredField becomeFirstResponder];
By making the field the first responder, it have focus and the keyboard will be displayed.
Set the first responder for your view to be the text field. This can be done in IB.
In swift:
desiredField.becomeFirstResponder()