开发者

how to check if a view is behind the keyboard on iphone

in my application i have a bunch of textlabels and textviews. Sometimes the textview is underneath the keyboard. My question is if there's a way to check if a textview is behind the keyboard to move it up. I already know how to move views up, and i know about the keyboardWillAppear notifications, but i don't know how to check if the view is behind the keyboard. The thing is that i don't want to move the textview if it's not underneath the kayboard. How can achieve that?

Tha开发者_如何学编程nks in advance.


I would do the check for first responder as shown above

[text isFirstResponder];

then I'd check to see if the bounds of the text field's bounds are less than 215 (because i think that's the max height of the keyboard) and accommodate from there. so all together it looks like:

if([text isFirstResponder]){
    if(text.bounds.y > 215){
       text.bounds.y = CGPointMake(text.bounds.y-(text.bounds.y-215));
    }
}


I think the only way to see this is to verify each UITextField and UITextView if it returns YES for

[_text isFirstResponder];

If any UITextField or UITextView is First Responder, than it means that the keyboard is on the bottom of the screen.

You can see the keyboard will appear by listening to UITextFieldDelegate and UITextViewDelegate ShouldBeginEditing events: for UITextField it is:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;// return NO to disallow editing.

and for UITextView it is:

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜