Iphone在ScrollView下点击TextField使文本筐不被键盘遮住

1.拖一个Scroll View视图填充View窗口,将Scroll View视图拖大一些,使其超出屏幕。

2.向Scroll View拖(添加)多个Label视图和Text View视图。

3.在.h头文件中添加如下代码:

  1. #import <UIKit/UIKit.h>   
  2.   
  3.   
  4. @interface ShowTextFiled : UIViewController {  
  5.     IBOutlet UIScrollView *myscrollview;  
  6.       
  7.     UITextField * currentTextfield;//当前的文本筐   
  8.     BOOL keyboardIsShown;  
  9. }  
  10. @property (nonatomic,retain)UIScrollView *myscrollview;  
  11.   
  12. @end  
#import <UIKit/UIKit.h>


@interface ShowTextFiled : UIViewController {
    IBOutlet UIScrollView *myscrollview;
    
    UITextField * currentTextfield;//当前的文本筐
    BOOL keyboardIsShown;
}
@property (nonatomic,retain)UIScrollView *myscrollview;

@end

4.在Interface Builer中,将每个Text Field的delegate连接到File‘s Owner。这一步很重要,因为它使得文本筐的各种事件如:textFieldDidBeginEditing。textFieldDidEndEditing等可被试图控制器处理。

5.在viewDidLoad方法里面修改ScrollView的内容大小:

  1. - (void)viewDidLoad  
  2. {  
  3.     //下面这两句话必须写,否则scrollview不可以动   
  4.     myscrollview.frame = CGRectMake(0, 0, 320, 460);  
  5.     [myscrollview setContentSize:CGSizeMake(320,651 )];  
  6.     [super viewDidLoad];  
  7.     // Do any additional setup after loading the view from its nib.   
  8. }  
- (void)viewDidLoad
{
    //下面这两句话必须写,否则scrollview不可以动
    myscrollview.frame = CGRectMake(0, 0, 320, 460);
    [myscrollview setContentSize:CGSizeMake(320,651 )];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

6.在View窗口显示以前注册两个通知,这两个通知可以告诉你键盘何时显示或消失,在viewWillAppear方法里面注册:

  1. //页面加载前调用的方法,注册两个通知:一个是键盘弹出来的通知,另外一个是键盘隐藏的通知,不同的通知调用不同的方法进行处理   
  2. -(void) viewWillAppear:(BOOL)animated{  
  3.     //键盘弹起的通知   
  4.     [[NSNotificationCenter defaultCenter]   
  5.      addObserver:self  
  6.      selector:@selector(keyboardDidShow:)   
  7.      name:UIKeyboardDidShowNotification  
  8.      object:self.view.window];  
  9.     //键盘隐藏的通知   
  10.     [[NSNotificationCenter defaultCenter]  
  11.      addObserver:self  
  12.      selector:@selector(keyboardDidHide:)   
  13.      name:UIKeyboardDidHideNotification   
  14.      object:nil];  
  15. }  
//页面加载前调用的方法,注册两个通知:一个是键盘弹出来的通知,另外一个是键盘隐藏的通知,不同的通知调用不同的方法进行处理
-(void) viewWillAppear:(BOOL)animated{
    //键盘弹起的通知
    [[NSNotificationCenter defaultCenter] 
     addObserver:self
     selector:@selector(keyboardDidShow:) 
     name:UIKeyboardDidShowNotification
     object:self.view.window];
    //键盘隐藏的通知
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(keyboardDidHide:) 
     name:UIKeyboardDidHideNotification 
     object:nil];
}

7.上面的通知中,当键盘弹起的时候会调用 keyboardDidShow方法,当键盘消失会调用keyboardDidHide方法,这两个方法定义如下:

  1. //键盘弹起后处理scrollView的高度使得textfield可见   
  2. -(void)keyboardDidShow:(NSNotification *)notification{  
  3.     if (keyboardIsShown) {  
  4.         return;  
  5.     }  
  6.     NSDictionary * info = [notification userInfo];  
  7.     NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];  
  8.     CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];  
  9.     CGRect viewFrame = [myscrollview frame];  
  10.     viewFrame.size.height -= keyboardRect.size.height;  
  11.     myscrollview.frame = viewFrame;  
  12.     CGRect textFieldRect = [currentTextfield frame];  
  13.     [myscrollview scrollRectToVisible:textFieldRect animated:YES];  
  14.     keyboardIsShown = YES;  
  15. }  
//键盘弹起后处理scrollView的高度使得textfield可见
-(void)keyboardDidShow:(NSNotification *)notification{
    if (keyboardIsShown) {
        return;
    }
    NSDictionary * info = [notification userInfo];
    NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];
    CGRect viewFrame = [myscrollview frame];
    viewFrame.size.height -= keyboardRect.size.height;
    myscrollview.frame = viewFrame;
    CGRect textFieldRect = [currentTextfield frame];
    [myscrollview scrollRectToVisible:textFieldRect animated:YES];
    keyboardIsShown = YES;
}

  1. //键盘隐藏后处理scrollview的高度,使其还原为本来的高度   
  2. -(void)keyboardDidHide:(NSNotification *)notification{  
  3.     NSDictionary *info = [notification userInfo];  
  4.     NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];  
  5.     CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];  
  6.     CGRect viewFrame = [myscrollview frame];  
  7.     viewFrame.size.height += keyboardRect.size.height;  
  8.     myscrollview.frame = viewFrame;  
  9.     keyboardIsShown = NO;  
  10. }  
//键盘隐藏后处理scrollview的高度,使其还原为本来的高度
-(void)keyboardDidHide:(NSNotification *)notification{
    NSDictionary *info = [notification userInfo];
    NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];
    CGRect viewFrame = [myscrollview frame];
    viewFrame.size.height += keyboardRect.size.height;
    myscrollview.frame = viewFrame;
    keyboardIsShown = NO;
}

8.重写TextField的三个事件方法:

当点击TextField视图时会促发如下方法:这个方法会将当前点击的文本筐赋值给currentTextField成员变量

  1. -(void)textFieldDidBeginEditing:(UITextField *)textFieldView{  
  2.     currentTextfield = textFieldView;  
  3. }  
-(void)textFieldDidBeginEditing:(UITextField *)textFieldView{
    currentTextfield = textFieldView;
}
当用户点击键盘中的Return键时,会促发如下方法:

  1. -(BOOL)textFieldShouldReturn:(UITextField *)textFieldView{  
  2.     [textFieldView resignFirstResponder];  
  3.     return NO;  
  4. }  
-(BOOL)textFieldShouldReturn:(UITextField *)textFieldView{
    [textFieldView resignFirstResponder];
    return NO;
}

上一个方法中的resignFirstResponder会隐藏键盘,这样会促发另外一个方法:这里将currentTextField设为nil

  1. -(void)textFieldDidEndEditing:(UITextField *)textFieldView{  
  2.     currentTextfield = nil;  
  3. }  
-(void)textFieldDidEndEditing:(UITextField *)textFieldView{
    currentTextfield = nil;
}

9.最后不要忘了在View窗口关闭之前移除前面注册的通知:

  1. //页面消失前取消通知   
  2. -(void)viewWillDisappear:(BOOL)animated{  
  3.     [[NSNotificationCenter defaultCenter]  
  4.      removeObserver:self  
  5.      name:UIKeyboardDidShowNotification  
  6.      object:nil];  
  7.       
  8.     [[NSNotificationCenter defaultCenter]  
  9.      removeObserver:self  
  10.      name:UIKeyboardDidHideNotification  
  11.      object:nil];  
  12. }  
//页面消失前取消通知
-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:UIKeyboardDidShowNotification
     object:nil];
    
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:UIKeyboardDidHideNotification
     object:nil];
}



生命在于运动,先去打球,晚上回来在继续,哈哈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值