我的视图底部有一个文本字段。当您单击编辑它时,键盘将其覆盖,您无法看到您正在输入的内容。我想创建一种方法,在编辑此文本字段时重新调整视图。
如果我通过界面生成器声明了文本字段,那么我知道我所要做的就是链接(ctrl 单击然后拖动)它。但是我有在代码中声明的文本字段,所以我想知道如何为它分配一个方法。
我想做的伪代码:
if (the text field is currently being edited){
call method: adjust view
}
我的视图底部有一个文本字段。当您单击编辑它时,键盘将其覆盖,您无法看到您正在输入的内容。我想创建一种方法,在编辑此文本字段时重新调整视图。
如果我通过界面生成器声明了文本字段,那么我知道我所要做的就是链接(ctrl 单击然后拖动)它。但是我有在代码中声明的文本字段,所以我想知道如何为它分配一个方法。
我想做的伪代码:
if (the text field is currently being edited){
call method: adjust view
}
在 .h 中实现 UITextFieldDelegate,如下所示:
@interface ShippingInfoViewController : UIViewController < UITextFieldDelegate>
并确保将您的 textField 的委托设置为self
然后您可以访问许多方法,例如
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
-(void) textFieldDidBeginEditing:(UITextField *)textField{
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
- (void)textFieldDidEndEditing:(UITextField *)textField {
和许多其他人。
创建文本字段时,将其委托设置为 self。
[textField setDelegate:self];
然后实现委托方法
- (void)textFieldDidBeginEditing:(UITextField *)textField {
//adjust view here
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
//move view back to correct position here
}
您还可以注册 UIKeyboardWillShow/UIKeyboardWillHide 或 UIKeyboardDidShow/UIKeyboardDidHide 消息并为此实现功能。
使用以下功能:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
[self moveTextViewForKeyboard:notification up:YES];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
[self moveTextViewForKeyboard:notification up:NO];
}
- (void)moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up {
NSDictionary *userInfo = [notification userInfo];
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
CGRect keyboardRect;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
if (up == YES) {
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.noteTextView.frame;
originalTextViewFrame = self.noteTextView.frame;
newTextViewFrame.size.height = keyboardTop - self.noteTextView.frame.origin.y - 10;
self.noteTextView.frame = newTextViewFrame;
} else {
// Keyboard is going away (down) - restore original frame
self.noteTextView.frame = originalTextViewFrame;
}
[UIView commitAnimations];
}
对 originalTextViewFrame 使用静态变量:
static CGRect originalTextViewFrame;
您必须为文本视图分配一个委托:
[textView setDelegate: self]
我假设,那self
是你的视图控制器,它需要符合UITextViewDelegate
协议。文本视图将在开始其编辑周期时调用textViewShouldBeginEditing:
/ 。textViewWillBeginEditing