键盘盖住输入框问题,包含中英文切换

本文介绍了一个iOS应用中处理键盘显示与隐藏时调整输入框位置的方法。通过监听键盘将要显示与隐藏的通知来获取键盘的高度,并据此调整输入框的位置,确保用户在键盘弹出时仍能清晰地看到输入区域。

只是修改了官方的demo

 

- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillShow:)  name:UIKeyboardWillShowNotificationobject:nil];

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil]; 

    inputview=[[UITextViewalloc] initWithFrame:CGRectMake(123,750, 111,44)];

    [inputviewsetBackgroundColor:[UIColorredColor]];

    [self.view addSubview:inputview];

}

 

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    

}

 

- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView {

    

    if (inputview.inputAccessoryView == nil) {

        [[NSBundlemainBundle] loadNibNamed:@"AccessoryView"owner:selfoptions:nil];

    }

    

    returnYES;

}

 

 

- (BOOL)textViewShouldEndEditing:(UITextView *)aTextView {

    [aTextView resignFirstResponder];

    returnYES;

}

 

#pragma mark -

#pragma mark Responding to keyboard events

 

- (void)keyboardWillShow:(NSNotification *)notification {    

    NSDictionary *userInfo = [notification userInfo]; 

    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    [UIViewbeginAnimations:nilcontext:NULL];

    [UIView setAnimationDuration:animationDuration];    

    inputview.frame=CGRectMake(123,keyboardRect.origin.y-64, 111,44);

    [UIViewcommitAnimations];

}

 

 

- (void)keyboardWillHide:(NSNotification *)notification {

    

    NSDictionary* userInfo = [notification userInfo];

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];    

    [UIViewbeginAnimations:nilcontext:NULL];

    [UIView setAnimationDuration:animationDuration];    

    inputview.frame = CGRectMake(123,750, 111,44);    

    [UIViewcommitAnimations];

}

 

转载于:https://www.cnblogs.com/sgdkg/archive/2013/01/24/2875435.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值