您的位置:首页 > 其它

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

2013-01-24 09:19 211 查看
1.拖一个Scroll View视图填充View窗口,将Scroll View视图拖大一些,使其超出屏幕。

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

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

[cpp]
view plaincopyprint?

#import <UIKit/UIKit.h>

@interface ShowTextFiled : UIViewController {
IBOutlet UIScrollView *myscrollview;

UITextField * currentTextfield;//当前的文本筐

BOOL keyboardIsShown;
}
@property (nonatomic,retain)UIScrollView *myscrollview;

@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的内容大小:

[cpp]
view plaincopyprint?

- (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.

}

- (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方法里面注册:

[cpp]
view plaincopyprint?

//页面加载前调用的方法,注册两个通知:一个是键盘弹出来的通知,另外一个是键盘隐藏的通知,不同的通知调用不同的方法进行处理

-(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];
}

//页面加载前调用的方法,注册两个通知:一个是键盘弹出来的通知,另外一个是键盘隐藏的通知,不同的通知调用不同的方法进行处理
-(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方法,这两个方法定义如下:

[cpp]
view plaincopyprint?

//键盘弹起后处理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;
}

//键盘弹起后处理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;
}


[cpp]
view plaincopyprint?

//键盘隐藏后处理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;
}

//键盘隐藏后处理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成员变量

[cpp]
view plaincopyprint?

-(void)textFieldDidBeginEditing:(UITextField *)textFieldView{
currentTextfield = textFieldView;
}

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

[cpp]
view plaincopyprint?

-(BOOL)textFieldShouldReturn:(UITextField *)textFieldView{
[textFieldView resignFirstResponder];
return NO;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textFieldView{
[textFieldView resignFirstResponder];
return NO;
}


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

[cpp]
view plaincopyprint?

-(void)textFieldDidEndEditing:(UITextField *)textFieldView{
currentTextfield = nil;
}

-(void)textFieldDidEndEditing:(UITextField *)textFieldView{
currentTextfield = nil;
}


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

[cpp]
view plaincopyprint?

//页面消失前取消通知

-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];

[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
}

//页面消失前取消通知
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];

[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
}






生命在于运动,先去打球,晚上回来在继续,哈哈。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐