Pages

Tuesday, August 13, 2013

Move View while keyboard appears

There are some cases, when you want to type in UITextField and also want to show that in screen. But keyboard hides your UITextField. You can use the following code for this


-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view
    
    CGRect rect = self.completeSingleView.frame;
    if (movedUp)
    {
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.completeSingleView.frame = rect;
    
    [UIView commitAnimations];
}

Now How to Use this

First of all set the <UITextFieldDelegrate> in class ".h" file and set UITextField delegate to self, Like if we have some textfield with name "loginID" then set the delegate to self like below

loginID.delegate = self;

Now use the below code, which call the upper function 

-(void)textFieldDidEndEditing:(UITextField *)sender
{
    if  (self.completeSingleView.frame.origin.y < 0)
        [self setViewMovedUp:NO];
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if  (self.completeSingleView.frame.origin.y >= 0)
        [self setViewMovedUp:YES];
}



No comments:

Post a Comment