我有一个带有UITableview和UITextView的应用程序,所以有时键盘是向上的。每当键盘打开时,我都无法访问底部的3-4个表格单元格,因为它们总是卡在键盘后面。如何使UITableView在显示键盘时仅占用键盘上方的空间?
发布于 2013-09-06 09:31:04
我通常使用https://github.com/michaeltyson/TPKeyboardAvoiding,它会自动解决您通常遇到的问题,但如果您想手动编写一些代码
使用表视图setContentOffset
或scrollToRowAtIndexPath
CGPoint scrollPt = CGPointMake(textFieldRect.origin.x, textFieldRect.origin.y);
[_tableView setContentOffset:scrollPt animated:YES];
UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
[_tableView scrollToRowAtIndexPath:[_tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
发布于 2013-09-06 10:34:03
我也遇到过同样的问题,下面是我的解决方案。希望能有所帮助。
- (void) keyboardWillShow: (NSNotification*) aNotification
{
[UIView animateWithDuration: [self keyboardAnimationDurationForNotification: aNotification] animations:^{
CGSize kbSize = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//change the frame of your talbleiview via kbsize.height
} completion:^(BOOL finished) {
}];
}
- (void) keyboardDidShow: (NSNotification*) aNotification
{
[UIView animateWithDuration: [self keyboardAnimationDurationForNotification: aNotification] animations:^{
CGSize kbSize = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//change the frame of your talbleiview via kbsize.height
} completion:^(BOOL finished) {
}];
}
- (void) keyboardWillDisappear: (NSNotification*) aNotification
{
[UIView animateWithDuration: [self keyboardAnimationDurationForNotification: aNotification] animations:^{
//restore your tableview
} completion:^(BOOL finished) {
}];
}
- (NSTimeInterval) keyboardAnimationDurationForNotification:(NSNotification*)notification
{
NSDictionary* info = [notification userInfo];
NSValue* value = [info objectForKey: UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration = 0;
[value getValue: &duration];
return duration;
}
添加和删除事件侦听器。
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(keyboardWillShow:)
name: UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(keyboardDidShow:)
name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(keyboardWillDisappear:)
name: UIKeyboardWillHideNotification object:nil];
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardWillShowNotification object: nil];
[[NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardDidShowNotification object: nil];
[[NSNotificationCenter defaultCenter] removeObserver: self name: UIKeyboardWillHideNotification object: nil];
}
方法是在一个键盘动画期间接收多个事件。
发布于 2013-09-06 08:49:04
试试这个:
CGRect frame = [tableViewer frame]; //assuming tableViewer is your tableview
frame.size.height -= 200; //200 may be a bit off, should be height of keyboard
[tableViewer setFrame:frame];
同样的事情,但在键盘消失时将-=更改为+=。
https://stackoverflow.com/questions/18648080
复制相似问题