In Xcode, make a button look like a text field or a textfield look like a button - objective-c

In Xcode (iOS), I have a screen with a combination of buttons and text fields. However, they look different. Is there a way that I can have a button look like a textfield? Or is there a way I can make a textfield act like a button (perform some action when pressed and don't bring up the keyboard)?
Thanks

You can try one of these:
place a transparent (custom with no image) button on top of the text field and attach the desired action to it
show your view in the implementation of the textFieldShouldBeginEditing: UITextFieldDelegate protocol method
Subclassing is also an option, but it will probably complicate your task too much.

If You want to perform an action and do not bring up the keyboard when pressed on UITextField You can do it like this:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// do here everything you want
NSLog(#"Pressed on TextField!");
[self.view endEditing:YES]; // Hide keyboard
return NO;
}
This function will be called every time when pressed on textField.
** Don't forget to delegate UITextField.
Note: With this You will face the problem: if keyboard is shown it will not hide, so You need also add -endEditing:YES.

You need to custom one of them ... or both
Exemple : create a class CustomTextField : UITextField
in this class use :
#import <QuartzCore/QuartzCore.h>
Play with style

Late answer, but nevertheless. I did this to make a UIButton look like a UITextField
#property (weak, nonatomic) IBOutlet UIButton *resolutionButton;
And in viewDidLoad:
dispatch_async(dispatch_get_main_queue(), ^{
[[self.resolutionButton layer] setBorderColor: [UIColor colorWithWhite:0.6f alpha:0.6f].CGColor];
self.resolutionButton.layer.cornerRadius = 5.0f;
self.resolutionButton.layer.borderWidth = 0.5f;
}

Related

How I can get the value of my UITextField?

how I can get the value of my UITextField ? When I declare my UITextField in the Storyboard, I know but like this, I don't know.
(sorry for my English, I'm French)
Thank you in advance for your answer.
//Ajout d'un Text Field
CGRect rectTF = CGRectMake(10,70,100,20); // Définition d'un rectangle
UITextField *articleSaisi = [[UITextField alloc] initWithFrame:rectTF];
articleSaisi.borderStyle = UITextBorderStyleLine;
articleSaisi.placeholder = #"Article";
[self.view addSubview: articleSaisi];
I am not quite sure whether you want to access the value or you want to associate your UI element to your code:
Try this if you are saying that you want to access the text value.
atricleSaisi.text
Try to control drag the UI element to either your corresponding .h or .m file so that it can create IBOutlet for you if you are saying that you want to connect your UI element to your code.
Highly recommend you go check the documentation.
if you want to know when the user pressed return, as per your comment, then you should create a UITextViewDelegate for your text view and define its textFieldShouldReturn method:
Discussion
The text field calls this method whenever the user taps the return button. You can use this method to implement any custom behavior when the button is tapped.
Also, give a look at textFieldDidEndEditing, which is called whenever there is a focus change and you should define to correctly handle user input.
Old answer:
If I do not understand you incorrectly, you want to create a UITextField programmatically (i.e., not through a Storyboard).
In this case, you should put you initialisation code inside the viewDidLoad method of your view controller and make sure that you define a property for the text field instead of a local variable:
#interface MyViewController : UIViewController
....
#property (nonatomic, strong) UITextField* articleSaisi;
...
#end
- (void)viewDidLoad {
CGRect rectTF = CGRectMake(10,70,100,20); // Définition d'un rectangle
self.articleSaisi = [[UITextField alloc] initWithFrame:rectTF];
self.articleSaisi.borderStyle = UITextBorderStyleLine;
self.articleSaisi.placeholder = #"Article";
[self.view addSubview:self.articleSaisi];
}
If you do so, you can access the text field value from any other method in the view controller through its text property:
self.articleSaisi.text

UITextField in aUIScrollView - inputView doesn't work

I have a UItextField in a UIScrollView. I assigned an inputView (a picker) on my UITextField but it doesn't work. When I click on my textfield, I get the keyboard instead of a picker. When I delete the UIScrollView it works well (I get the picker). Do you have any ideas? Thanks.
Edit (more clear): I have a UItextField in a UIScrollView. I call a method that shows a picker. But the method is never called when my textfield is in a scrollview and I get the keyboard. When I delete the UIScrollView, my method (show picker) is called and I get my picker.
In case you are already using the textfield delegate, try ...
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self openCustomPicker:self]; // Call your IBAction method
return NO; // Hide both keyboard and blinking cursor.
}
to prevent the UITextField from showing the keyboard.

UIVIew endEditing:YES doesnt hide the keyboard

I have a UIVIew which is a subview and it contains several UITextFields. One of these textfields (which is for DATE) should not be editable using the keyboard, instead of this I use a PopOver with a Datepicker inside.
I run a method when the UIControlEventEditingDidBegin is reached. This method calls the resignFirstResponder on the DateTextField.
Everything works fine if the DateTextField is the first field to edit, but when another textField is edited and of course shows the keyboard and then try to edit the DateField, the keyboard doesn't hide and everything goes normal but with the Keyboard doing anything.
I have tried to call the method endEditing:YES before the resignFirstResponder but it doesn't work. I have tried to run the endEditing:YES and resignFirstResponder on the didEndEditing text field method but theres no way to get that keyboard out.
here is my method:
- (void)showDatePopOver:(id)sender{
[self.view endEditing:YES];
UITextField *textField = (UITextField *)sender;
[sender resignFirstResponder]; // hide keyboard
/** POP OVER LINES**/
}
You should use the textFieldShouldBeginEditing: delegate method instead of resigning first responder in didBeginEditing:
This will allow editing on ALL BUT the dateTextField text field:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return (![textField isEqual:dateTextField]);
}
You should specify that your view controller is a text view delegate as well like so (in the interface declaration [.h file]):
#interface MyViewController : UIViewController <UITextFieldDelegate>

TextField will NOT resign first responder with UIModalPresentationFormSheet view

I've created a button on one viewController that loads another view modally using the UIModalPresentationFormSheet presentation style. On this loaded view, I have two textFields, and I'm forcing the first textField to be first responder so that the keyboard will appear immediately with the new view. I've set up the textFields to have an action method that is hooked up to "Did End on Exit" event. However, whenever I hit "return" on the keyboard for either textField, the keyboard fails to go away (Here is my code):
// addCustomPage method that is called when button from original view is touched
- (IBAction) addCustomPage:(id)sender
{
NSLog(#"Adding Custom Page");
if (!self.customPageViewController)
{
self.customPageViewController =
[[CustomPageViewController alloc] initWithNibName:#"CustomPageViewController" bundle: nil];
}
customPageViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:customPageViewController animated:YES];
// force keyboard to appear with loaded page on the first textField
[customPageViewController.firstTextField becomeFirstResponder];
}
#interface CustomPageViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *firstTextField;
#property (strong, nonatomic) IBOutlet UITextField *secondTextField;
- (IBAction)keyboardEndOnExit:(id)sender; // DID END ON EXIT EVENT
#end
//in CustomPageViewController.m
-(IBAction)keyboardEndOnExit:(id)sender
{
[sender resignFirstResponder];
}
This is a fairly straight forward problem, and I have no problem normally dismissing keyboards using this technique with basic views and textFields. I'm not sure if using a view in this presentation format, or set up makes things different. Thanks!
You have confirmed that you keyboardEndOnExit method is actually being called?
You could also take a more direct approach by calling [yourTextView resignFirstResponder] when a specific action is take by the user, such as a key pressed etc. I would still check if that method is ever being called using breakpoints or a log.
Have a look at this question. Pretty sure it is the same problem caused by UIModalPresentationFormSheet.

Bring to front DatePicker on an UITextField

When an UITextField is firstResponder, I would like to bring to front an UIDatePicker (bottom part of the screen) without the "going down keyboard" (no call to UITextField resignFirstResponder).
The aim is to process like UIKeyboard of UITextField which pop-up on nearly everything when it becomeFirstResponder. modalViewController seems to be fullscreen only.
- showDatePicker:(id)sender {
if([taskName isFirstResponder]) [taskName resignFirstResponder];
[self.view.window addSubview: self.pickerView];
// size up the picker view and compute the start/end frame origin
(...)
[UIView commitAnimations];
}
This example is an animation of keyboard going down, and DatePicker going up, behind and not in front.
Do you know a solution ? A piece of code would be welcome. Thanks in advance.
This is simply done by setting the input view of the text field to the Picker View. Then, on Editing did begin tell the picker view to becomeFirst responder. Like this
textField.inputView = pickerView
then using an IBAction called when the Editing Did Begin
-(IBAction) setPickerViewAsFirstResponder:(id)sender
{
[pickerView becomeFirstResponder];
}
This works perfectly. You'll need to implement code to actually set what the picker view is currently showing to be a string in the text field still.
This definitely can be done... simply implement the method below after setting UIViewController <UITextFieldDelegate> in your .h
Long story short, this overrides the keyboard loading before text editing begins.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Make a new view, or do what you want here
UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,245,0,0)];
[self.view addSubview:pv];
return NO;
}