UIView animation not working on text begin editing in iphone app - iphone

I am making the iPad app in which I want to show animation when I start click on beginEditing here is the my code.
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"This is calling");
[self showAnimationPests];
}
-(void) showAnimationPests
{
[UIView animateWithDuration:0.5
animations:^{
subView.frame = CGRectMake(0,-200,1024,748);
}];
}
It shows Log this is calling but view does not move.

If it's an iphone app why is the size of it 1024x748?
CGRectMake takes points not pixels (if you are trying to make up for retina display). And those points for iPhone 5 are 320x568 and previous devices 320x480.

try with my bellow code
Example..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
subView.frame = CGRectMake(0,-200,1024,748);
[UIView commitAnimations];
return YES;
}
and set to 0 like default in textFieldShouldReturn: method like bellow..
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
subView.frame = CGRectMake(0,0,1024,748);
[UIView commitAnimations];
return YES;
}

Related

Keep UIScrollView in correct place after switching from landscape to portrait

I have a UIScrollView that contains a text field and a text view. I have code to move the text fields up when the keyboard is present so the keyboard does not cover the text fields. This code works great in portrait view:
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField) {
[textField resignFirstResponder];
}
return NO;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.nameField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 90), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == self.nameField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 90), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textViewDidBeginEditing:(UITextView *)textField {
if (textField == self.questionField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 200), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textViewShouldEndEditing:(UITextView *)textField {
if (textField == self.questionField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 200), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
When I rotate the iPhone simulator into a landscape view, this (not surprisingly) does not work. How can I get the text field to move up just enough to see it while typing into it in both landscape and portrait views?
Also, if I rotate from landscape to portrait while the keyboard is shown, after dismissing the keyboard the scroll view has moved down the screen instead of lined up in its original position. How can I avoid this?
Apple's document Managing the Keyboard shows the proper way to do this under the heading "Moving Content That Is Located Under the Keyboard".
You basically put your content on a scroll view and use the UIKeyboardDidShowNotification and UIKeyboardWillHideNotification notifications to adjust the content offsets.
However, the code is somewhat incomplete. The keyboard size calculation doesn't work in landscape mode. To fix it, replace this:
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
With this:
// Works in both portrait and landscape mode
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect toView:nil];
CGSize kbSize = kbRect.size;

IPhone TextView hidden by keyboard

I have a UITextView editable when selected appears the keyboard. The problem is that when the text is so big he gets behind the keyboard. How can I solve this problem?
move your view up by this code
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//change origin y
[UIView beginAnimations:nil context:self.view];
[UIView setAnimationDuration:0.25];
[self.view setFrame:CGRectMake(0,-150,320,436)];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
//reset origin y
[UIView beginAnimations:nil context:self.view];
[UIView setAnimationDuration:0.25];
[self.view setFrame:CGRectMake(0,0,320,436)];
[UIView commitAnimations];
}
set x and y value according your requirement.
change base view size when edit UITextField,decrease base view orgin origin y to edit text field
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//change origin y
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
//reset origin y
}
Use the delegate methods of UITextfield.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:self.view];
[UIView setAnimationDuration:duration];
//change origin y
[self.view setFrame:CGRectMake(X,newY,width,height)];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:self.view];
[UIView setAnimationDuration:duration];
//reset origin y
[self.view setFrame:CGRectMake(X,Y,width,height)];
[UIView commitAnimations];
}
Don't forget to set the UITextFieldDelegate in your ".h" file and the appropriate text fileds textfiled.delegate = self.

Moving Text Fields during input

I have set up some text fields and labels to look like a simple form which will have to get some user input. My problem is however that whenever some of the text fields are selected and the keyboard slides out, the text fields are covered making it impossible to see the input. My question is how can I move up the text fields so that they stay in view when selected. Maybe someone who is more experienced can help me on this.
A good solution for this which I like to do is listen to when the textfield begins editing with the following delegate function:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//keep a member variable to store where the textField started
_yPositionStore = textField.frame.origin.y;
//If we begin editing on the text field we need to move it up to make sure we can still
//see it when the keyboard is visible.
//
//I am adding an animation to make this look better
[UIView beginAnimations:#"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];
commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
160 , //this is just a number to put it above the keyboard
commentTextField.frame.size.width,
commentTextField.frame.size.height);
[UIView commitAnimations];
}
and then in this callback you can return it to its original location:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView beginAnimations:#"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];
commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
_yPositionStore ,
commentTextField.frame.size.width,
commentTextField.frame.size.height);
[UIView commitAnimations];
}
}
You will need to declare _yPositionStore as a member variable of CGFloat and have your textFields delegate be set to the view controller that owns it(either using Interface builder and dragging delegate to files owner or setting yourTextField.delegate = self)
I hope that helps
Do do this you need to follow next 2 steps:
- put all your UITextViews into UIScrollView
- register for keyboard notifications and slide your UIScrollView when keyboard appears
Both steps explained here: "Moving Content That Is Located Under the Keyboard"
You can call delegate methods of UITextfield and customize it according to requirement.
- (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 = 110;
const float movementDuration = 0.3f;
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: #"aimation" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
yourView.frame= CGRectOffset(yourView.frame,0, movement);
[UIView commitAnimations];
}
For my applications I use the following code assuming that keyboardHeight is 216 for portrait and 162 for landscape mode:
#pragma mark - textField delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// keyboard is visible, move views
CGRect myScreenRect = [[UIScreen mainScreen] bounds];
int keyboardHeight = 216;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35];
int needToMove = 0;
CGRect frame = self.view.frame;
if (textField.frame.origin.y + textField.frame.size.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height > (myScreenRect.size.height - keyboardHeight)) {
needToMove = (textField.frame.origin.y + textField.frame.size.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height) - (myScreenRect.size.height - keyboardHeight);
}
frame.origin.y = -needToMove;
[self.view setFrame:frame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
// resign first responder, hide keyboard, move views
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35];
CGRect frame = self.view.frame;
frame.origin.y = 0;
[self.view setFrame:frame];
[UIView commitAnimations];
}
This formula takes into account the status bar, navigation bar and display size. And of course do not forget to set the delegate for your fields.
And with:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
you can hide the keyboard, when the user tap outside the field.

Keyboard hides UITextField. How to solve this?

I have a textField which is at bottom of the screen. When I begin editing, the keyboard appears and hides the textField.
Can anyone suggest how to solve this?
Generally you would put your content in a scroll view and move your scroll view up when the keyboard appears. There are many tutorials out there so google them. Here is one of them.
You can use following code in two diffrent method, call it when editing began and end, to move your view up and down,(just change the valuew that is been subtracted in yourView.frame.origin.y-100 to adjust according to your need)
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];
yourView.frame = CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y-100, yourView.frame.size.width, yourView.frame.size.height);
[UIView commitAnimations];
See this code snippet. And use one UIButton to dismiss UIKeyboard.
- (void) animateTextField: (UITextField *)textField up:(BOOL) up
{
const int movementDistance = 200;
const float movementDuration = 0.4f;
int movement = (up ? -movementDistance : movementDistance);
up ? (self.button.enabled= YES) : (self.button.enabled = NO);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
-(void)textFieldDidBeginEditing:(UITextField*) inTextField
{
[self animateTextField:mText up:YES];
[self addObservers];
}
- (void)addObservers
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(textFieldDidChange:) name:#"UITextFieldTextDidChangeNotification" object:nil];
}
- (void)textFieldDidChange:(NSNotification*)aNotification
{
NSLog(#"TextField data=%#",mText.text);
}
- (IBAction) doneBtn:(id)sender
{
[mText resignFirstResponder ];
[self animateTextField:mText up:NO];
}
When you tap on textField keyboard up then your textField hide by it. so waht you need, you need to make your view up not keyboardresign is a solution. so for making you view up use
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField delegate for the logic. now see the code
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField.tag==1) //add tag to your textField to identify it
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
CGRect viewFrame=self.view.frame;
viewFrame=sinViewFrame.size.height+100;
viewFrameorigin.y-=100; // as required
self.view.frame=viewFrame;
[UIView commitAnimations];
}
}
hope it helps you.
I use auto layout constraint to solve this problem. I hold constraint as a property, and play with it when keyboard appears / disappears. I simply move views above the textfield out of screen, and textfield to the top. After finishing edit, I move them by the constraint to their original position.
-(void)keyboardWillShow:(NSNotification*)notification{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
self.topSpaceLayoutConstraint.constant = -150;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
} completion:NULL];
}
-(void)keyboardWillHide:(NSNotification*)notification
{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
self.topSpaceLayoutConstraint.constant = 10;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
} completion:NULL];
}
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpaceLayoutConstraint;

animate textlabel in uitableviewcell using willTransitionToState

I am trying to animate the textlabel in a UItableviewcell when I press the edit button.
I am trying to make it fade out and fade in.
fading in works but when I press 'edit' the textlabel disappears and when I press on 'done' I fades in just perfectly.
Can anyone tell me why it isn't working?
thanks in advance
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if ((state & UITableViewCellStateEditingMask) || (state & UITableViewCellStateShowingDeleteConfirmationMask)) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
label.alpha = 0.0;
[UIView commitAnimations];
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
[super didTransitionToState:state];
if (!(state & UITableViewCellStateEditingMask) && !(state & UITableViewCellStateShowingDeleteConfirmationMask)) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
label.alpha = 1.0;
[UIView commitAnimations];
}
}
I noticed that when entering willTransitionToState that animations were disabled. The following fixed it.
- (void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
//Should be enabled by default...but apparently not
[UIView setAnimationsEnabled:YES];
...
}
From everything I had read I thought for sure the willTransitionToState was the way to go. It even works perfectly if you use didTransitionToState though the transition starts after the normal editing transition finishes.
As it turns out I think you want to use setEditing
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
if(editing) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
label.alpha = 0.0;
[UIView commitAnimations];
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
label.alpha = 1.0;
[UIView commitAnimations];
}
}