gpt4 book ai didi

ios - 在常规 UIViewController 的 UITableViewCell 中将 UITextField 滚动到键盘上方

转载 作者:行者123 更新时间:2023-11-28 07:31:03 25 4
gpt4 key购买 nike

我已经尝试过 StackOverflow 上的大部分示例。我也用过苹果的。我对他们的问题似乎是他们不考虑 UITableView 中的 UITextField。我已经这样做了很多次,但不是以这种方式。我有一个带有 UITextField 的自定义 UITableViewCell。

在我的 UITableView(不是 UITableViewController)上,我需要能够避免将 UITextField 隐藏在 UITableView 下。

我现在有这个:

-(void)viewDidLoad
{
....
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];

....
}

- (void)scrollToRectOfTextField {
UITableViewCell *cell = (UITableViewCell*)[self.activeTextField superview];
CGRect r = CGRectMake(self.activeTextField.frame.origin.x,
cell.frame.origin.y+self.activeTextField.frame.origin.y,
self.activeTextField.frame.size.width,
self.activeTextField.frame.size.height);
[self.tableView scrollRectToVisible:r animated:YES];
}

- (void)keyboardDidShow:(NSNotification *)notification
{
if (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone) {
NSDictionary *userInfo = [notification userInfo];
CGSize size = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
NSLog(@"TableView: %@", NSStringFromCGRect(self.tableView.frame));
CGRect newTableViewFrame = CGRectMake(self.tableView.frame.origin.x,
self.tableView.frame.origin.y,
self.tableView.frame.size.width, self.tableView.frame.size.height - size.height);
self.tableView.frame = newTableViewFrame;
NSLog(@"New TableView: %@", NSStringFromCGRect(self.tableView.frame));


self.tableView.contentSize = CGSizeMake(self.tableView.contentSize.width, self.tableView.contentSize.height-size.height);
}
}


- (void)keyboardWillHide:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
CGSize size = [[userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x,
self.tableView.frame.origin.y,
self.tableView.frame.size.width,
self.tableView.frame.size.height + size.height);

NSLog(@"%@", NSStringFromCGRect(self.tableView.frame));
}


-(void)textFieldDidBeginEditing
{
[self scrollToRectOfTextField];
}

这似乎将一堆空白推到我的键盘后面。另请注意,我的 UITextField 上也有一个 inputAccessoryView。

哦,我不能使用任何第三方类。我喜欢 TPKeyboardAvoidingScrollView,但我不能使用任何第三方。

最佳答案

我花了一整天的时间试图解决这个问题。我把它贴在这里,然后找到了一个博客链接和一个非常简单的解决方案。它看起来像这样:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:self.tableView];
CGPoint contentOffset = self.tableView.contentOffset;

contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);

NSLog(@"contentOffset is: %@", NSStringFromCGPoint(contentOffset));

[self.tableView setContentOffset:contentOffset animated:YES];

return YES;
}


-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];

if ([textField.superview.superview isKindOfClass:[UITableViewCell class]])
{
UITableViewCell *cell = (UITableViewCell*)textField.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:TRUE];
}

return YES;
}

Check this for iOS 8

关于ios - 在常规 UIViewController 的 UITableViewCell 中将 UITextField 滚动到键盘上方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54661792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com