Programmatically adding UITextField to a dropdownlist - ios

I have a tableview controlled by a UITableViewController.
In this UITableViewController class I have added a UITextField programmatically to my UINavigationItem by adding these lines to my viewDidLoad method :
CGRect passwordTextFieldFrame = CGRectMake(20.0f, 100.0f, 280.0f, 31.0f);
UITextField *passwordTextField = [[UITextField alloc] initWithFrame:passwordTextFieldFrame];
passwordTextField.placeholder = #"Password";
passwordTextField.backgroundColor = [UIColor whiteColor];
passwordTextField.textColor = [UIColor blackColor];
passwordTextField.font = [UIFont systemFontOfSize:14.0f];
passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
passwordTextField.returnKeyType = UIReturnKeyDone;
passwordTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
passwordTextField.tag = 2;
passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
passwordTextField.delegate = self; // ADD THIS LINE
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:passwordTextField];
The UITextField is displayed correctly on my navigationbar but when I click it the textFieldDidBeginEditing never fires.
I have added the to my header file along with the textFieldDidBeginEditing method.
I tried doing the exact same thing in a view controlled by UIViewController and here the textFieldDidBeginEditing gets fired when I click the "textfield" - So I suspect the fact that I am adding the textfield to a UITableViewController is what's causing trouble.
You might think - why didn't you add a "uinavigationbutton" to the "NavigationBar" instead - but I can't as the uidropdownmenu that I want to call when the button is being clicked only accepts a "UITextField".

Try adding one more line:
[passwordTextField becomeFirstResponder];

Related

How to I add a button on top of iCarousel?

I added the date label and the share button via code. Now I want to add a static button in the right top corner to open a slide out menu(I'm using MMDrawerController). If I'm adding the via code, it's moving with the carousel.
If I'm adding a button on the name label(My App) on the storyboard, it's not getting clicked and the carousel gesture is being registered instead of the button click.
How should I proceed to add a button for a slide out menu on the right ?
-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
UIView *magView = nil;
CGFloat height = self.myCarousel.bounds.size.height;
CGFloat width = self.myCarousel.bounds.size.width;
//Magazine View
magView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, width/1.35, height/1.75)];
magView.backgroundColor = [UIColor clearColor];
UIImageView *magImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"4 1:2.jpg"]];
magImage.frame = CGRectMake(0, 0, width/1.35, height/1.75);
[magView addSubview:magImage];
//Date Label
UILabel *dateLabel = nil;
dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, height/1.7, width/4, height/25)];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.text = #"12-03-2017";
dateLabel.font = [UIFont fontWithName:#"Roboto-Black" size:5];
dateLabel.textColor = [UIColor whiteColor];
//Share Button
UIButton *shareButton = nil;
shareButton = [[UIButton alloc] initWithFrame:CGRectMake(width/1.49, height/1.7, height/25, height/25)];
shareButton.backgroundColor = [UIColor clearColor];
[shareButton setBackgroundImage:[UIImage imageNamed:#"share_icon_1x"] forState:UIControlStateNormal];
[magView addSubview:dateLabel];
[magView addSubview:shareButton];
return magView;
}
Simulator
Storyboard
How do I proceed to add the button ?
Add button to storyboard and after adding carousel programmatically. add code to bring the button to top on view.
[self.view bringSubviewToFront:button];
if you are adding it programmatically then just after [self.view addSubview:carousel];
call [self.view bringSubviewToFront:button](make sure button is added on view before calling it)
I solved the issue. Instead of taking the view of the ViewController I dragged/dropped another view onto the ViewController and displayed the carousel on it. Now I can set the button easily. Thanks for the help though. Cheers
It will help you
[xyzButton.superview setUserInteractionEnabled:YES];
:)

textFieldShouldReturn does not get called (textField loaded programmatically)

I'm currently loading a -UITextField programmatically and trying to make the keyboard go down once I click on 'Search'.
I declare the -UITextFeild inside the .m file as
UITextField *searchTextField;
Inside the .h file I do indeed declare the -UITextField's delegate
#interface firstChannel : UIViewController<LBYouTubePlayerControllerDelegate, UITableViewDelegate,UITableViewDataSource, NSXMLParserDelegate, UITextFieldDelegate, AVPlayerItemOutputPullDelegate>{
This is how I add the UITextField programmatically.
searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, -26, 300, 30)];
searchTextField.borderStyle = UITextBorderStyleRoundedRect;
searchTextField.font = [UIFont fontWithName:#"Heiti TC" size:13];
searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
searchTextField.placeholder = #"Search on the Channel";
searchTextField.borderStyle = UITextBorderStyleNone;;
searchTextField.textAlignment = UITextAlignmentCenter;
searchTextField.center = CGPointMake(160, 43);
searchTextField.textColor = [UIColor colorWithRed:(232.0/255.0) green:(232.0/255.0) blue:(232.0/255.0) alpha:(100.0/100.0)];
searchTextField.clearButtonMode = UITextFieldViewModeNever;
searchTextField.returnKeyType = UIReturnKeySearch;
[searchTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.view.window addSubview:searchTextField];
[searchTextField becomeFirstResponder];
I added the delegate inside -viewDidLoad
searchTextField.delegate = self;
However when I click on "Search" on the keyboard nothing happens. I'm unsure why that's happening I've even debugged it by logging something once you hit returns but nothing happens.
Call the searchTExtField.delegate after you create and setup the text field.
Try moving all the code in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad]; searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, -26, 300, 30)];
searchTextField.borderStyle = UITextBorderStyleRoundedRect;
searchTextField.font = [UIFont fontWithName:#"Heiti TC" size:13];
searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
searchTextField.delegate = self;
searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
searchTextField.placeholder = #"Search on the Channel";
searchTextField.borderStyle = UITextBorderStyleNone;;
searchTextField.textAlignment = UITextAlignmentCenter;
searchTextField.center = CGPointMake(160, 43);
searchTextField.textColor = [UIColor colorWithRed:(232.0/255.0) green:(232.0/255.0) blue:(232.0/255.0) alpha:(100.0/100.0)];
searchTextField.clearButtonMode = UITextFieldViewModeNever;
searchTextField.returnKeyType = UIReturnKeySearch;
[searchTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.view.window addSubview:searchTextField];
[searchTextField becomeFirstResponder];
}
Also, if you are using search bar then you need to use search bar delegate. If you are just using UITextfield, then you should be okay.
Have you implemented the delegate method (this method gets called when the user taps the return key):
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
//Perform search with text:
[self performSearch:[textField text]];
return NO;
}
This will resign the responder. Here you can also call your search method.

iOS - Implementing a Class for UITextField

I have a UIView, with lots of UITexfields,
UITextField *field1 = [[UITextField alloc] initWithFrame:CGRectMake(135, 292, 50, 20)];
field1.backgroundColor = [UIColor clearColor];
field1.borderStyle = UITextBorderStyleNone;
field1.returnKeyType = UIReturnKeyNext;
field1.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
field1.font = [UIFont fontWithName:inputFont size:fontSize];
field1.placeholder = #"size";
[self addSubview:field1];
Can I create a UITextField Class and create an instance of this class with these parameters preset? -so as to reduce the code: (If I have lots of fields the code gets very long and repetitive? -
Can i get some help on how about to set up this class! -thank you
Yes You Can do this by making a BaseUITextFeild .h and .m file as shown in the following image.
And then Just add the following code regarding your design in BaseUITextFeild.m file
- (void)awakeFromNib {
[self setupUI];
[self setKeyboardAppearance:UIKeyboardAppearanceAlert];
}
// Function to setup the common UI throught the app.
- (void)setupUI {
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self setTextAlignment:NSTextAlignmentCenter];
[self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:#"_placeholderLabel.textColor"];
[self setFont:[UIFont boldSystemFontOfSize:15.0f]];
}
Generating a custom UITextField programmatically
And then just import "BaseUITextFeild.h" in the ViewController Where you want to use it.
BaseUITextFeild *field1 = [[BaseUITextFeild alloc] initWithFrame:CGRectMake(135, 292, 50, 20)];
[self addSubview:field1];
Generating a custom UITextField From Interface Builder(Storyboard/.XIB)
Add a UITextfield and then just Change the Class to your Custom TextView Class.
By Applying Following technique you can use this same UITextFeild through out the Application, Just need to import BaseUITextFeild.h.
You can also use this UITextFieldCategory
Follow this Two Example UITextField+withFrame.h and UITextField+ColoredPlaceholder.h.
You can also customize you UITextField as per your need.
Create a method
-(UITextField*)createTextFieldWithFrame:(CGRect)frame{
UITextField *textField = [[UITextField alloc] initWithFrame:frame];
textField.backgroundColor = [UIColor clearColor];
textField.borderStyle = UITextBorderStyleNone;
textField.returnKeyType = UIReturnKeyNext;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
textField.placeholder = #"size";
[self.view addSubview:textField];
return textField;
}
for creating multiple textfields call this method
UITextField *text1 = [self createTextFieldWithFrame:CGRectZero];
UITextField *text2 = [self createTextFieldWithFrame:CGRectZero];
You can create a custom xib with the UITextField created as you want and reuse that every time you need.
Or you can subclass UITextField and do the additional setup inside.

Custom UITextField created programmatically doesnt bring up keyboard on tap

I have a custom look designed for my textfields on iphone. I decided to create a class
MYTextField : UIView <UITextFieldDelegate>
I have a method
- (id)initWithPlaceholder:(NSString*)placeholder position:(CGPoint) position
that creates a UITextField with the desired view and look.
The sourcecode is as follows:
- (id)initWithPlaceholder:(NSString*)placeholder position:(CGPoint) position {
if (self = [super initWithFrame:CGRectMake(position.x, position.y, TEXTFIELD_WIDTH, TEXTFIELD_HEIGHT)]) {
UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)] autorelease];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[textField setUserInteractionEnabled:YES];
textField.delegate = self;
[self addSubview:textField];
}
return self;
}
The problem is that, on invoking the method, the UITextField is visible but on tapping on the textfield, the keyboard doesnt show up.
Is there anything I am missing?
PS: I found a couple of similar problems on Stackoverflow, but none of the solutions seem to work for me. :(
The issue here could be that the textfields frame doesn't lie within the frame of the UIView. Why use TEXTFIELD_WIDTH for the self.frame but not for textfield.frame?
Also, why subclass UIView rather that UITextField?

Why doesn't the clear button aligned with the text in a UITextField?

I have an UITextField in an UITableViewCell.
For some reason the clear button isn't align to the textField's text.
Here's my textField code:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(22, 10, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
usernameField.adjustsFontSizeToFitWidth = YES;
usernameField.placeholder = #"Username";
usernameField.backgroundColor = [UIColor clearColor];
usernameField.keyboardType = UIKeyboardTypeDefault;
usernameField.autocapitalizationType = UITextAutocorrectionTypeNo;
usernameField.autocorrectionType = UITextAutocorrectionTypeNo;
usernameField.returnKeyType = UIReturnKeyNext;
usernameField.delegate = self;
usernameField.clearButtonMode = UITextFieldViewModeAlways;
[usernameField setEnabled:YES];
[cell addSubview:usernameField];
[usernameField becomeFirstResponder];
[usernameField release];
And here's the picture to demonstrate the problem:
Edit:
I also tried with - (CGRect)clearButtonRectForBounds:(CGRect)bounds and still nothing
What is your contentVerticalAlignment property? I've seen this issue show up when the clear button is center aligned but the text is aligned the top. Try setting your textfield alignment to the following:
usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Any luck with that?

Resources