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.
Related
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];
}
I'm having a problem with FBDialog.
Apparently after the either the email or the password textFields become first responders and keyboard comes up, the FBDialog view no longer reacts to changing the interface orientation.
Except for the case when keyboard is up, all works as expected.
Did anyone ever encounter this behavior?
Thanks in advance, Vlad
Try this:
(void)deviceOrientationDidChange:(void*)object
{
UIDeviceOrientation orientation = (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation;
if ([self shouldRotateToOrientation:orientation])
{
[self updateWebOrientation];
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
[self sizeToFitOrientation:YES];
[UIView commitAnimations];
}
}
I've created a custom UIAlertView (by subclassing it and messing around with its show function) that has some custom subviews and is of non-standard size.
It works ok when I create and display it, however, when the device is rotated, the alert rotates and then returns to its default size.
Any ideas what functions to override - or should I tweak the UIViewController?
thanks,
Peter
Not sure if force rotating the UIAlertView fits the Apple GUI guidelines, but you can rotate it by defining the status bar (status bar and UIAlertView sticks together)
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
application.statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
But UIAlertView is a UIView just like many others, so try this :
- (void)didPresentAlertView:(UIAlertView *)alertView
{
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:0.1];
alertView.transform = CGAffineTransformRotate(alertView.transform, degreesToRadian(90));
[UIView commitAnimations];
}
I am working on an iPhone Application and am attempting to get a Landscape View for a simple, editable UITextView working.
Both interface layouts are specified in NIB files, and I have succeeded in loading them and migrating data when the devices rotates.
However, the UITextView in the Landscape version "runs away" from the iPhone Keyboard, making it impossible to see what you're editing. I have no idea why it is doing this--can anybody help?
Thanks in Advance!
VIDEO OF PROBLEM: http://www.youtube.com/watch?v=hfWBKBA_KjQ
No way to know what you are doing without some code, but you should try something like this:
- (void) animateTextField: (UITextView*) textView 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];
}
Call this from where you think appropiate (UIKeyboardWillShowNotigication, for instance) and well, it will animate your view in order to show the textview.
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.