ios关于键盘遮挡的简单方法

1 利用UIScrollview 的设置使得整体上移

把控件都放在scrollview上面,然后利用当用户点击的时候,设置scrollview 的CGPointMake(scroll.contentOffset.x, y+100)就可以达到效-(void)textFieldDidB

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    UIScrollView *scroll=(UIScrollView *)self.superview;
    float y = 200;
    [scroll setContentOffset:CGPointMake(scroll.contentOffset.x, y+100) animated:YES];
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    
    UIScrollView *scroll = (UIScrollView *)self.superview;
    [scroll setContentOffset:CGPointMake(scroll.contentOffset.x, 0) animated:YES];
    return;
}
记得这些都是放在scrollview上的控件,

2 第二中的方法是利用就是把父类的view整体上移,

#pragma mark textKeyBoardHeight method
//先响应Textfiled方法,因为有多个控件,所以利用tag区分
-(void)textFieldDidBeginEditing:(UITextField *)textField{

    [self slideFrameUp:nil withTag:textField.tag];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    [self slideFrameDown:nil withTag:textField.tag];
}
//设置上移还是下降
-(void) slideFrameUp:(id)up withTag:(NSInteger)tag
{
    [self slideFrame:YES withTag:tag];
}

-(void) slideFrameDown:(id)down withTag:(NSInteger)tag
{
    [self slideFrame:NO withTag:tag];
}

-(void) slideFrame:(BOOL) up withTag:(NSInteger)tag
{//设置上移的多少
    int movementDistance=1;
    if (tag==3) {
        movementDistance =70;
    }else if (tag==4){
        movementDistance = 100; // 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.frame = CGRectOffset(self.frame, 0, movement);
    [UIView commitAnimations];
}
上面就是简单的不被键盘遮挡的方法,


posted on 2013-05-04 23:17  songbai  阅读(284)  评论(0编辑  收藏  举报