gpt4 book ai didi

objective-c - 滚动 UIScrollView 以显示几个文本字段中的最低部分

转载 作者:太空狗 更新时间:2023-10-30 03:54:12 30 4
gpt4 key购买 nike

我有一个 UIScrollView,里面有几个动态 View ,每个 View 都有一个文本字段。当我开始在其中一个框中键入内容时,我希望 ScrollView 滚动,以便该字段位于屏幕顶部(在键盘上方可见)。效果很好;这是代码:

(void)didStartTyping:(id)sender {
[scrollView setContentOffset:CGPointMake(0, subView.frame.origin.y) animated:YES];
scrollView.scrollEnabled = NO;
}

(void)didFinishTyping:(id)sender {
scrollView.scrollEnabled = YES;
}

但是,每当 ScrollView 向上滚动到最顶部并且我开始在最低的可见文本字段中键入内容时,它就不会向下滚动足够远(短约 40 像素)。令人费解的是,如果我从 ScrollView 的顶部向下滚动一个像素,它就可以工作,但是当我向上滚动到顶部时,它的行为却大不相同。

最佳答案

我设法做到这一点的最好方法是捕获键盘框架,然后在 TextView 调用 textViewDidBeginEditing: 时更新我的​​ ScrollView 插入。我在这里使用的是 tableview,但相同的逻辑应该适用于 scrollview,主要区别在于滚动方式。我使用 scrollToRowAtIndexPath,你会想要使用 scrollRectToVisible

//setup keyboard callbacks
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillHideNotification
object:nil];
}

- (void)keyboardWillShow:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
kbFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
}

//this is called from your UITextViewDelegate when textViewDidBeginEditing: is called
- (void)updateActiveTextScroll:(UITextView*)textView
{
activeTextView = textView;
UIEdgeInsets inset;
UIInterfaceOrientation orient = [[UIApplication sharedApplication] statusBarOrientation];
if( UIInterfaceOrientationIsLandscape(orient) )
{
inset = UIEdgeInsetsMake(0.0, 0.0, kbFrame.size.width, 0.0);
}
else
{
inset = UIEdgeInsetsMake(0.0, 0.0, kbFrame.size.height, 0.0);
}
myTableView.contentInset = inset;
myTableView.scrollIndicatorInsets = inset;

[myTableView scrollToRowAtIndexPath:activeNSIndexPath
atScrollPosition:UITableViewScrollPositionBottom
animated:YES];
}

//dont forget to reset when the keyboard goes away
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets inset = UIEdgeInsetsZero;
myTableView.contentInset = inset;
myTableView.scrollIndicatorInsets = inset;
}

关于objective-c - 滚动 UIScrollView 以显示几个文本字段中的最低部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10907617/

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