gpt4 book ai didi

ios - 显示键盘时 UITableview 不会向上滚动

转载 作者:行者123 更新时间:2023-12-01 18:57:58 25 4
gpt4 key购买 nike

我有一个这样的开始 View :

enter image description here

其中屏幕的前半部分是谷歌地图,后半部分是 UITableview,标题单元格中有一个 UISearchbar。

当我点击搜索栏时,这发生在大屏幕上,但在小屏幕上,我的 uisearchbar 被键盘覆盖。

enter image description here

如您所见,当键盘出现时,搜索栏会稍微移到底部。我不想要那个。它应该将所有内容向上推,以便 Tableview 可见。

我遵循了关于 contentInset 的教程。但它什么也没做。

- (void)viewDidLoad
{
[super viewDidLoad];


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

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

- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
NSLog(@"KeyboardWillshow");
// Step 1: Get the size of the keyboard.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;


// Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;


// Step 3: Scroll the target text field into view.
CGRect aRect = self.view.frame;
aRect.size.height -= keyboardSize.height;
if (!CGRectContainsPoint(aRect, _searchBar.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, _searchBar.frame.origin.y - (keyboardSize.height-15));
[self.tableView setContentOffset:scrollPoint animated:YES];
NSLog(@"Scrolled");
}
}

- (void)keyboardWillHide:(NSNotification *)notification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
}

它出现在打印 Scrolled 的 if 测试中。仍然没有任何 react 。

其中 _searchBar 是我的 UISearchbar 变量。

任何人的想法?

编辑:

解决了除了 UISearchbar 点击时会向下跳的问题。这会产生一个差距,就像您在屏幕截图中看到的那样:

enter image description here

最佳答案

请参见下面的示例。

- (void)keyboardWasShown:(NSNotification*)aNotification

{

NSDictionary* info = [aNotification userInfo];

CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;


CGRect aRect = self.view.frame;

aRect.size.height -= kbSize.height;

float height =150;
float yPoint = aRect.size.height-height;


[self.tblSearch setFrame:CGRectMake(self.tblSearch.frame.origin.x
, yPoint, self.tblSearch.frame.size.width, height)];



}

关于ios - 显示键盘时 UITableview 不会向上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25377828/

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