UITextField Won't Change Frame on Refocus - iphone

I'm having a peculiar problem. I have a view with two UITextFields that start out 280px wide. On focus, I want them to shorten to reveal a button - I'm doing that with the following code:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect revealButton = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 221, textField.frame.size.height);
[UIView beginAnimations:nil context:nil];
textField.frame = revealButton;
[UIView commitAnimations];
NSLog(#"%f",textField.frame.size.width);
}
Once editing has ended, they should go back to their original frame:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect hideButton = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 280, textField.frame.size.height);
[UIView beginAnimations:nil context:nil];
textField.frame = hideButton;
[UIView commitAnimations];
}
The first time I focus a text field, it works perfectly. However, if I focus the first text field after focusing something else (for example, if I focus the first text field initially, focus the second, and then refocus the first, or if I initially focus the second and then focus the first), it simply won't change its frame. Even more puzzling is the fact that it will log 221 as its width - it just won't show that on the screen. Furthermore, this problem doesn't apply to the second text field.
Any ideas? Thanks in advance...

That's strange, I ran a quick test using two text fields with the exact same code and works every time.
I'd suggest deleting the text fields and connections and rebuild them. Clean all targets and try again.
Edit according to your comments:
If you're using Auto Layout you must not modify the frame of the text fields directly. The actual frames of UI elements are calculated by the system.
For your purpose I'd suggest to set up a width constraint for every text field. Make sure that you only have a left or right spacing constraint not both in addition to the width constraint. To animate it use the following code:
- (NSLayoutConstraint *)widthConstraintForView:(UIView *)view
{
NSLayoutConstraint *widthConstraint = nil;
for (NSLayoutConstraint *constraint in textField.constraints)
{
if (constraint.firstAttribute == NSLayoutAttributeWidth)
widthConstraint = constraint;
}
return widthConstraint;
}
- (void)animateConstraint:(NSLayoutConstraint *)constraint toNewConstant:(float)newConstant withDuration:(float)duration
{
[self.view layoutIfNeeded];
[UIView animateWithDuration:duration animations:^{
constraint.constant = newConstant;
[self.view layoutIfNeeded];
}];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
float newWidth = 221.0f;
NSLayoutConstraint *widthConstraint = [self widthConstraintForView:textField];
[self animateConstraint:widthConstraint toNewConstant:newWidth withDuration:0.5f];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
float newWidth = 280.0f;
NSLayoutConstraint *widthConstraint = [self widthConstraintForView:textField];
[self animateConstraint:widthConstraint toNewConstant:newWidth withDuration:0.5f];
}

Related

iPad keyboard hiding textfield

This is a very common problem in which the keyboard hides the textfield . Also there is lots of solution posted on SO for this.
So currently i am referring following post which is working well in iPad portrait mode but in iPad landscape mode the the view is sliding towards left direction where as i want the view to move up.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 80; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
As iPad itself providing good options like "Undock" and "Split" for Keyboard, generally we dont need to arrange Text Field. Although considering you problem if you needed check device current orientation
using [[UIDevice currentDevice] orientation] based on that animate text fields frame.

Scrolling in a table

in my table I use cells with UITextField as subview. I also can edit it but only for the upper cells in the table I also see, what I edit, because the keyboard hides the lower cells.
Now what must I do, to scroll the cell I whant to edit move to the top?
I tried
selectRowAtIndexPath:indexPathOfCurrrentCell animated:NO scrollPostion:UITableViewSchrollPositionTop
and
scrollToRowAtIndexPath:idexPathOfCurrentCell atScrollPosition:UITableViewSchrollPositionTop animated:NO
but none of it works. Must I use an other command or add something additional? What must I change?
Thanks
If you can't manually scroll the tableView to somewhere where the cell is visible; the code won't either.
The solution is to set the frame of the tableView to have a height that respects the keyboard's height of 170 points.
You could try something like this:
tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.frame.size.height-170);
Do this in your method which gets called when the textField becomes the first responder.
Take a look at this post. I think you'll find what you need.
Get UITableView to scroll to the selected UITextField and Avoid Being Hidden by Keyboard
Use Delegate Method of UITextField
Even I encountered this scrolling problem and i use the below logic to get rid of this.
This Works fine
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField==textFieldOfYourInterest )
{
CGRect frame = self.view.frame;
if ([textField isFirstResponder] && self.view.frame.origin.y >= 0)
{
frame.origin.y -= 70;
}
else if (![textField isFirstResponder] && self.view.frame.origin.y < 0)
{
frame.origin.y += 70;
}
[self.view setFrame:frame];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
CGRect frame = self.view.frame;
if (![topTextFieldName isFirstResponder] && self.view.frame.origin.y < 0)
{
frame.origin.y += 70;
}
[self.view setFrame:frame];
NSLog(#"text field should return");
[textField resignFirstResponder];
return YES;
}
If any problem revert to me.
Thanks
Nagaraja Sathe

Hide Show UIPicker on button click

I want to do something like the keyboard show/hide so I want to set up picker as a first responder. Is this possible. If not how can i show/hide UIPicker in a view by a button click. On other topic how can i format the DatePicker output, now i get "2011-12-8 23:00 000000"
You could have your UIPicker below your sceen (say, y = 480 for iPhone), and then when you tap your button move it to 480 - picker.frame.size.height so that it shows right above the bottom of your screen.
You can achieve the animated effect by using UIView animations as:
// Pre OS4 way
[UIView beginAnimations:#"animName" context:NULL];
[UIView setAnimationDuration:0.2];
CGRect f = picker.frame;
if (showingPicker) {
f.origin.y = 480;
}
else {
f.origin.y = 480 - picker.frame.size.height;
}
picker.frame = f;
[UIView commitAnimations];
// Post OS4 way - Block based
[UIView animateWithDuration:0.2 animations:^{
if (showingPicker) {
f.origin.y = 480;
}
else {
f.origin.y = 480 - picker.frame.size.height;
}
picker.frame = f;
}
And for the DatePicker output you can use an NSDateFormatter. You can use predefined date and time styles or use setDateFormat:(NSString *)format to specify the format yourself.
After that all you do is call your [formatter stringFromDate:datePicker.date];
You can find a complete source code how to use UIDataPicker here.

Setting Up Textfield Animationd To Move TextFields Above Keyboard

I know there is alot on this topic already. I got the code below from another question, but I have no idea how to set it up to use. Can someone give me a detailed step by step on how to actually setup the process of moving a textfield above the keyboard when the keyboard comes up then moving it back when the editing is done.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 80; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
Is the textFieldDidBeginEditing: or textFieldDidEndEditing: ever get called?
If not, you might not setting your text field's delegate correctly.
When you declare you text field (or, if you're using IB, in viewDidLoad), add this:
yourTextField.delegate = self;
I would highly recommend using a UITableViewController and put your UITextField into the table. That way the keyboard-hiding issue is solved for you by the system.

keyboard hiding textfields in tableViewController

I am using a tableViewController having 2 sections ....
1st section has 7 rows & 2nd section has 2 rows.
So when i edit in textfields of 2nd section keyboard hides these field so how i will handle
keyboard for these fields only.(i am using tableviewController which has default scrolling).
I am new to objective -C.....Your help please.
Thanks.
On tapping any cell with text field, you can navigate to another view with only a text field, keyboard and Done and Cancel buttons. this feels pretty neat. You can see this in many of apple's iphone apps as well... e.g. Go to iPhone Settings -> Mail, Contacts, Calendars -> Signature.
EDIT: since you cant use the standard way you can move the complete view up with following code:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
CGPoint currentCenter = [self.view center];
CGPoint newCenter = CGPointMake(currentCenter.x, currentCenter.y - 150);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[self.view setCenter:newCenter];
[UIView commitAnimations];
return YES;
}
change the animation duration and the change in position of the center (150) in this code according to your requirements. I have assumed that your view controller is the delegate for your textfield.
To bring the view back to it's original position, use same code but add to center.y in textFieldShouldReturn or textFieldShouldEndEditing etc
CGPoint newCenter = CGPointMake(currentCenter.x, currentCenter.y - 150);
P.S. : I'm still not sure you should be using this second approach.