scrollview上的textfile随键盘的改变而改变位置
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (void) keyboardShow:(NSNotification *) notif
{
// 获得键盘尺寸
NSDictionary *info = notif.userInfo;
NSValue *aValue = [info objectForKeyedSubscript:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
if (scrollNum==0) {
//重新定义ScrollView的尺寸
CGRect viewFrame = self.scrollView.frame;
viewFrame.size.height -=(keyboardSize.height); //原来的尺寸减去键盘的高度
self.scrollView.frame = viewFrame;
//获取当前文本框架大小
CGRect textFileRect = [textFile frame];
//滚动到当前文本框
[self.scrollView scrollRectToVisible:textFileRect animated:YES];
scrollNum=1;//防止多个textfile来回切换时多次执行
}
}
- (void) keyboardChange:(NSNotification *) notif
{
// 获得键盘尺寸
NSDictionary *info = notif.userInfo;
NSValue *aValue = [info objectForKeyedSubscript:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
//当键盘隐藏的时候,将scrollView重新放下来
CGRect viewFrame = self.scrollView.frame;
viewFrame.size.height += keyboardSize.height;
self.scrollView.frame = viewFrame;
scrollNum=0;
}