我的UIViewController
包含UITableView
个自定义单元格,单元格内部为UILabels
,有两个不可编辑的UITextView
和一个可编辑的UITextView
。现在,当我点击桌子底部或底部附近的UITextView
之一时,键盘会覆盖UITextView
。我已经尝试了http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html,这对于textfield / textview很有用,但是没有使用自定义单元格在桌面上工作。有任何帮助或建议如何解决这个问题?
答案 0 :(得分:1)
<p>
答案 1 :(得分:0)
两种解决方案:
首选:使用UITableViewController而不是UIViewController,因为它会自动确保您的键盘不会隐藏可编辑字段。
Hacky :How to make a UITextField move up when keyboard is present?
答案 2 :(得分:0)
一个简单的解决方案。实现heightForFooter
方法,让它返回(比方说)100的值,当您在UITableView
中选择一个单元格时,它们只会向上滑动该高度,键盘将无法覆盖风景。
答案 3 :(得分:0)
我总是使用双折解决方案。
要做到这一点,我会注册键盘显示/隐藏事件,并在被调用时采取相应的行动。
- (void)keyboardWillShow:(NSNotification *)note {
[self updateForKeyboardShowHide:note appearing:YES];
}
- (void)keyboardWillHide:(NSNotification *)note {
[self updateForKeyboardShowHide:note appearing:NO];
}
- (void)updateForKeyboardShowHide:(NSNotification *)note appearing:(BOOL)isAppearing {
// ignore notifications if our view isn't attached to the window
if (self.view.window == nil)
return;
CGFloat directionalModifier = isAppearing?-1:1;
CGRect keyboardBounds = [[note.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat animationDuration = [[note.userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// figure out table re-size based on keyboard
CGFloat keyboardHeight;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(orientation))
keyboardHeight = keyboardBounds.size.height;
else
keyboardHeight = keyboardBounds.size.width;
[UIView animateWithDuration:animationDuration animations:^{
// resize table
CGRect newFrame = table.frame;
newFrame.size.height += [self calculateKeyboardOffsetWithHeight:keyboardHeight] * directionalModifier;
table.frame = newFrame;
} completion:^(BOOL finished){
// scroll to selected cell
if (isAppearing) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:textFieldInEdit.tag inSection:0];
[table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}];
}
- (CGFloat)calulateKeyboardOffsetWithHeight:(CGFloat)keyboardHeight {
// This depends on the size and position of your table.
// If your table happen to go all the way to the bottom of
// the screen, you'll needs to adjust it's size by the whole keyboard height.
// You might as well ditch this method and inline the value.
return keyboardHeight;
// My table did not go to the bottom of the screen and the position was
// change dynamically so and there was long boring calculation I needed to
// do to figure out how much my table needed to shrink/grow.
}
答案 4 :(得分:0)
当您的表格视图包含UITextField
或UITextView
等数据输入字段且表格视图足以覆盖屏幕时,您将无法访问被隐藏的数据输入字段键盘。
为了克服这个问题,有两种解决方案:
最简单且推荐的方法是使用UITableViewController
代替UIViewController
,这会自动确保键盘不会隐藏可编辑字段(如果可能,请使用此方法避免UI调整不便)
如果您使用UIViewController
和UITableView
作为其子视图。您可以通过观察UIKeyboardWillShowNotification
和UIKeyboardWillHideNotification
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil]; //Posted immediately prior to the display of the keyboard
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil]; //Posted immediately prior to the dismissal of the keyboard.
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
CGRect keyboardBounds = [[[aNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0); //when keyboard is up, that time just bring your text filed above the keyboard
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
[self.tableView scrollToRowAtIndexPath:[self findIndexPathToScroll]
atScrollPosition:UITableViewScrollPositionTop
animated:YES]; //findIndexPathToScroll implementation not shown
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
self.tableView.contentInset = UIEdgeInsetsZero; //Once keyboard is hidden then bring back your table into your original position.
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
[UIView commitAnimations];
}
registerForKeyboardNotifications
- 加载UITableView时调用此方法,即:viewDidLoad
findIndexPathToScroll
- (实现未显示)它是您的业务逻辑,用于准备表视图应滚动的IndexPath
removeObserver
'{1}}和dealloc