打开键盘时,我有一个UIViewController
包含自定义细胞UITableView
,在细胞内是UILabels
,一对夫妇的不可编辑UITextView
和一个可编辑UITextView
。现在,当我点击靠近桌子底部或底部的UITextView
之一时,UITextView
被键盘覆盖。我试过http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html,它对textfield/textview非常有用,但不能用自定义单元格工作。任何帮助或建议如何去做这件事?避免视图从一个UITextView一个UITableView内与自定义单元格
1
A
回答
0
两个解决方案:
首选:使用一个UITableViewController,而不是一个UIViewController作为一个会自动确保您的键盘不会隐藏编辑字段。
哈克:How to make a UITextField move up when keyboard is present?
0
一个简单的解决方案。实现heightForFooter
方法,并让它返回(比如说)100的值,并且当您选择UITableView
中的单元格时,它们将仅向上滑动该高度,并且键盘不会覆盖该视图。
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.
}
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
- (未显示试行),是你的业务逻辑准备索引路径表视图应滚动removeObserver
'UIKeyboardWillShowNotification'和'UIKeyboardWillHideNotif ication”无论是在dealloc
和viewDidUnload
1
I fixed the issue. Please see my solution below:
1. First declare a global varibale called "activeFileld"
@property(nonatomic,strong)id activeFiled;
2. Create a method called "registerForKeyboardNotifications"
- (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.
}
3. Called the above method in viewWillAppear:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//Register kryboard Notification
[self registerForKeyboardNotifications];
}
4. Call the Delegate method for UitextFieldd Or UitextView
- (void)textFieldDidBeginEditing:(UITextField *)sender {
self.activeField = sender;
}
- (void)textFieldDidEndEditing:(UITextField *)sender{
self.activeField = nil;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
// save the text view that is being edited
_notes = textView.text;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
// release the selected text view as we don't need it anymore
_activeField = nil;
}
5.
- (void)keyboardWillShow:(NSNotification *)notification
{
if([_activeField isKindOfClass:[UITextField class]]) {
NSDictionary* info = [notification userInfo];
NSLog(@"Dictionary %@",info);
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect fromView:nil];
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbRect.size.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.view.frame;
aRect.size.height -= kbRect.size.height;
UITextField *textField = (UITextField*)_activeField;
if (!CGRectContainsPoint(aRect, textField.frame.origin)) {
[self.tableView scrollRectToVisible:textField.frame animated:YES];
}
}else if([_activeField isKindOfClass:[UITextView class]]) {
NSDictionary* info = [notification userInfo];
NSLog(@"Dictionary %@",info);
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect fromView:nil];
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbRect.size.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.view.frame;
aRect.size.height += kbRect.size.height;
UITextView *activeTextView = (UITextView*)_activeField;
if (!CGRectContainsPoint(aRect, textField.superview.superview.frame.origin)) {
[self.tableView scrollRectToVisible:activeTextView.superview.superview.frame animated:YES];
}
}
}
// Called when the UIKeyboardWillHideNotification is received
- (void)keyboardWillHide:(NSNotification *)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
}
相关问题
- 1. 从一个自定义单元格
- 2. 慢UITableView与UIScrollView和PageControl在每个自定义单元格内
- 3. Uitableview单元格自定义唯一ID
- 4. UITableView自定义视图和“加载更多”在同一个单元格
- 5. 自定义uitableview单元格?
- 6. UITableView与自定义单元格
- 7. 在一个自定义的UIViewController包含一个UITableView子视图
- 8. UITableview单元格保留自定义图像视图过滤器
- 9. RxSwift表视图与多个自定义单元格类型
- 10. UITableView与两个自定义单元格没有正确显示
- 11. UITableView在5个单元格后失败的自定义绘图
- 12. iPhone - UITableView显示两个不同的自定义单元格视图
- 13. 在一个UITableView单元中显示一个xib视图
- 14. 像uitextview自定义uitableview
- 15. 具有单个自定义单元格的UITableView? (iPhone)
- 16. 如何从子视图自定义单元格Refersh UI视图?
- 17. UITableView与自定义单元格一次只加载一行数据
- 18. 自定义单元格的表视图
- 19. 在单元格内选择自定义UITextView时选择单元格
- 20. 从cellForRowAtIndexPath返回一个自定义单元格:
- 21. UILabels和自定义的UITableView单元格
- 22. UITableView的自定义单元格滚动
- 23. 自定义UITableView不刷新单元格
- 24. UITableView 2自定义单元格
- 25. 自定义单元格在UITableView
- 26. 自定义UITableView(UIView +单元格)
- 27. UITableView和自定义单元格
- 28. Xcode 4.2 UITableview自定义单元格
- 29. UITableView自定义单元格非常慢
- 30. 在UITableView单元格内自动生长UITextView
好,我不能使用的UITableViewController因为我得到了很多的东西,这个视图中,而不是仅仅的UITableView。我会尝试另一个。我稍后会回到这里。 – Diffy