gpt4 book ai didi

ios - 如何让 UITableView 向上滚动以容纳键盘?

转载 作者:行者123 更新时间:2023-11-28 20:47:17 29 4
gpt4 key购买 nike

谁能解释如何让这些方法起作用?它们编译得很好,但什么都不做。

我想让我的 tableView 滚动,这样我的键盘就可以很好地适应。

针对此问题发布的解决方案是重置 contentSizes 和订阅 NSNotfication。这三种方法看起来应该可以满足我的要求,但什么也没有发生。

[self.myTable setContentOffset:CGPointMake(0, -100) animated:YES];

[self.myTable selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

[self.myTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

最佳答案

您发布的代码非常不完整,无法调试您做错了什么,因为它只包含所需内容的一小部分。请看看这是否是您正在寻找的解决方案?

在您的 init 方法中添加以下内容:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

在你的实现中添加这个(也添加到头文件中):

- (void)keyboardWillShow:(NSNotification *)notification {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2
NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
#else
NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];
#endif
CGRect keyboardBounds;
[keyboardBoundsValue getValue:&keyboardBounds];
UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);
[[self tableView] setScrollIndicatorInsets:e];
[[self tableView] setContentInset:e];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2
}
#endif
}

最后在您的dealloc 方法中,添加:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

此代码来自 Ben Copsey 的 ASIHTTPRequest 的 ASIAuthenticationDialog 类 wrapper 。希望对您有所帮助。

关于ios - 如何让 UITableView 向上滚动以容纳键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4788729/

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