gpt4 book ai didi

ios - 自动移动 UIScrollView 区域的可见性

转载 作者:行者123 更新时间:2023-11-28 21:30:06 26 4
gpt4 key购买 nike

如果出现键盘,我想让我的 View 自动向上移动。已经使用苹果的代码here它运作良好。

这就是我管理对象的方式,因此我创建了一个覆盖 UIViewUIScrollView。此 UIViewUITextFieldUIButton 组成。

Document Outline

这是我在出现键盘时调整 View 的方式。

#pragma mark - Keyboard Handling

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];

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

}

// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_scrollView.contentInset = contentInsets;
_scrollView.scrollIndicatorInsets = contentInsets;

// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _mainView.frame.origin) ) {
[self.scrollView scrollRectToVisible:_mainView.frame animated:YES];
}
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrollView.contentInset = contentInsets;
_scrollView.scrollIndicatorInsets = contentInsets;
}

但我认为有一点很奇怪。当键盘出现时,它会滚动并且我的 UITextField 变得可见。但我认为它太紧了。

Result

在我看来,如果我的 UITextField 向上移动一点会更好。我的问题是,如何设置它的滚动可见性?看起来应该在这里添加一些变量和一些常量

CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _mainView.frame.origin) ) {
[self.scrollView scrollRectToVisible:_mainView.frame animated:YES];
}

注意:我想要的结果 Expectation

非常感谢,如果有一点提示,我们将不胜感激。

最佳答案

最简单的解决方案是在键盘打开时向上移动 View (或 ScrollView )。

- (void)keyboardWillShow:(NSNotification*)notification{
[self.view setFrame:CGRectMake(0,-100, self.view.frame.size.width, self.view.frame.size.height)]; // where 100 is the offset
[self.view setNeedsDisplay];

}

- (void)keyBoardWillHide:(NSNotification*)notification{
[self.view setFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view setNeedsDisplay];
}

关于ios - 自动移动 UIScrollView 区域的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36444065/

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