gpt4 book ai didi

ios - ScrollView 存在键盘时文本字段消失

转载 作者:行者123 更新时间:2023-11-28 22:25:14 24 4
gpt4 key购买 nike

我在 UiscrollView 中出现键盘时遇到问题。

我添加了一个 UIScrollview 作为

scrlView=[[UIScrollView alloc] initWithFrame:CGRectMake(10, 140, 1000, 600)];
scrlView.scrollEnabled=YES;
scrlView.showsVerticalScrollIndicator=YES;
scrlView.bounces=NO;

我在这个 ScrollView 中添加了 10 行 UITextFields,每行有 5 个文本字段,每个文本字段的高度为 50px。每当尝试编辑文本字段时,它都会被键盘重叠。为此,我尝试了这段代码

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


- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = selectetTxtfld.superview.frame;
bkgndRect.size.height += kbSize.height;
[selectetTxtfld.superview setFrame:bkgndRect];
[scrlView setContentOffset:CGPointMake(0.0, selectetTxtfld.frame.origin.y) animated:YES];
}

//发送 UIKeyboardWillHideNotification 时调用

 - (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;

[UIView animateWithDuration:0.4 animations:^{
scrlView.contentInset = contentInsets;
}];
scrlView.scrollIndicatorInsets = contentInsets;
}

但是textField没有出现在键盘上。它出现在 ScrollView 的ypoint位置

帮我解决这个问题。我在 StackOverFlow 中看到了很多答案。但没有解决我的问题

最佳答案

- (void)keyboardWillBeHidden:(NSNotification*)aNotification {

NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat offSetAfterKeyboardIsDisplayed = scrlview.contentOffset.y + kbSize.height;

[UIView animateWithDuration:0.3 animations:^{
//adding content inset at the bottom of the scrollview
scrlView.contentInset = UIEdgeInsetMake(0,0,kbSize.height,0);
[scrlview setContentOffset:offSetAfterKeyboardIsDisplayed]
}];
}


- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{

NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat offSetAfterKeyboardResigns = scrlview.contentOffset.y - kbSize.height;

[UIView animateWithDuration:0.3 animations:^{
scrlView.contentInset = UIEdgeInsetsZero;
[scrlview setContentOffset:offSetAfterKeyboardResigns]
}];
}

关于ios - ScrollView 存在键盘时文本字段消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19187588/

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