我在UiscrollView中出现键盘问题。
我添加了一个UIScrollview
scrlView=[[UIScrollView alloc] initWithFrame:CGRectMake(10, 140, 1000, 600)];
scrlView.scrollEnabled=YES;
scrlView.showsVerticalScrollIndicator=YES;
scrlView.bounces=NO;
到这个scrollView我添加了10行UITextFields,每行有5个textFields,每个textfield高度为50px。 当试图编辑文本字段时,它被keyBoard重叠。因为我尝试了这段代码
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = selectetTxtfld.superview.frame;
bkgndRect.size.height += kbSize.height;
[selectetTxtfld.superview setFrame:bkgndRect];
[scrlView setContentOffset:CGPointMake(0.0, selectetTxtfld.frame.origin.y) animated:YES];
}
}
//发送UIKeyboardWillHideNotification时调用
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
[UIView animateWithDuration:0.4 animations:^{
scrlView.contentInset = contentInsets;
}];
scrlView.scrollIndicatorInsets = contentInsets;
}
但是textField没有出现在键盘上。它出现在scrollview ypoint位置
帮我解决这个问题。我在StackOverFlow中看到很多答案。但是没有解决我的问题
答案 0 :(得分:1)
: 1.add内容插入滚动视图底部,其值等于键盘的高度。 2. setContentOffset =当前偏移+键盘的高度。 注意:1& 3应在动画块中完成,持续时间等于0.30
在keyboardWillBeHidden中: 1.set contentInset = UIEdgeInsetsZero 2. setContentOffset =当前偏移量 - 键盘的高度。 注意:1& 3应在动画块中完成,持续时间等于0.30
这应该解决你的问题:)
答案 1 :(得分:1)
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat offSetAfterKeyboardIsDisplayed = scrlview.contentOffset.y + kbSize.height;
[UIView animateWithDuration:0.3 animations:^{
//adding content inset at the bottom of the scrollview
scrlView.contentInset = UIEdgeInsetMake(0,0,kbSize.height,0);
[scrlview setContentOffset:offSetAfterKeyboardIsDisplayed]
}];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat offSetAfterKeyboardResigns = scrlview.contentOffset.y - kbSize.height;
[UIView animateWithDuration:0.3 animations:^{
scrlView.contentInset = UIEdgeInsetsZero;
[scrlview setContentOffset:offSetAfterKeyboardResigns]
}];
}