xcode 出现键盘时如何向上移动 UIScrollView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27521617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to move up a UIScrollView when keyboard appears?
提问by Cuero
Update:
更新:
my question is: how to prevent the scrollview being reset to the position when clicking on another textfield?
我的问题是:如何防止在单击另一个文本字段时将滚动视图重置到该位置?
Actually I've tried several ways, such as the solution to this question.
其实我已经尝试了几种方法,比如这个问题的解决方案。
Originally, my scrollView (multiple textfield and textview in it, and all of positions are placed in storyboard) is at the position (0, 302). When the keyboard appears, I wanna move it up to (0, 100). Firstly, I tried to add a button and do the following, it works.
最初,我的滚动视图(其中有多个文本字段和文本视图,并且所有位置都放置在故事板中)位于 (0, 302) 位置。当键盘出现时,我想将其向上移动到 (0, 100)。首先,我尝试添加一个按钮并执行以下操作,它有效。
CGRect frame = self.informationScrollView.frame;
frame.origin.y = 100;
self.informationScrollView.frame = frame;
Then I tried to do the same code in the selector of UIKeyboardDidShowNotification, but it failed. I found the position are always reset even when the activity textField has changed. Can anyone tell me how to prevent the app reseting the position? Thanks a lot.
然后我尝试在 UIKeyboardDidShowNotification 的选择器中执行相同的代码,但失败了。我发现即使活动 textField 已更改,位置也始终会重置。谁能告诉我如何防止应用程序重置位置?非常感谢。
回答by Mrunal
CGRect frame = self.informationScrollView.frame;
[UIView animateWithDuration:1
delay:0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
self.informationScrollView setFrame:CGRectMake (frame.origin.x,
frame.origin.y,
frame.size.width,
<change height as per requirement>);
}
completion:nil];
For Multiple TextFields:
对于多个文本字段:
How to make a UITextField move up when keyboard is present?
Hope this helps.
希望这可以帮助。
回答by iAnurag
If ypu are using a UIScrollView then, you should take a look in TPAvoidingScrollView. Its easy to use and give the expected result. You just have to set TPAvoidingScrollView as your UIScrollView's superclass and everything will be taken care of by TPScrollView. JUST TRY IT. HOPE THIS HELPS.
如果 ypu 正在使用 UIScrollView,那么您应该查看TPAvoidingScrollView。它易于使用并提供预期的结果。您只需将 TPAvoidingScrollView 设置为 UIScrollView 的超类,TPScrollView 将处理所有事情。就试一试吧。希望这可以帮助。
回答by Sarat Patel
Its pretty simple Use this into your viewDidLoad
method
它非常简单 将此用于您的viewDidLoad
方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardDidHideNotification object:nil];
and write a code for scrollView against keyboard height inside that
并针对其中的键盘高度编写 scrollView 的代码
-(void)keyboardWasShown:(NSNotification*)notification
{
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(self.view.frame.origin.x,self.view.frame.origin.y, kbSize.height+100, 0);
self.scrollViewChildDetail.contentInset = contentInsets;
self.scrollViewChildDetail.scrollIndicatorInsets = contentInsets;
}
-(void)keyboardWillBeHidden:(NSNotification *)notification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollViewChildDetail.contentInset = contentInsets;
self.scrollViewChildDetail.scrollIndicatorInsets = contentInsets;
}
it works.
有用。
回答by Sarat Patel
Use the best concept of NSNotificationCenter
which is explained by @Sarat_Patel
使用NSNotificationCenter
@Sarat_Patel 解释的最佳概念
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardDidHideNotification object:nil];
And handle with local methods:
并使用本地方法处理:
-(void)keyboardWasShown:(NSNotification*)notification
-(void)keyboardWillBeHidden:(NSNotification *)notification
回答by cotugs
The best course of action with UIScrollView is to set the contentInsetsand the scrollIndicatorInsetsbased on the appearance of the keyboard. If the content of the scroll view are your UITextField and UITextView instances, then you should be offsetting the scroll view content via contentOffset, not moving the frame of the scroll view.
使用 UIScrollView 的最佳做法是根据键盘的外观设置contentInsets和scrollIndicatorInsets。如果滚动视图的内容是您的 UITextField 和 UITextView 实例,那么您应该通过contentOffset偏移滚动视图内容,而不是移动滚动视图的框架。
That being said, if you really need to move the scroll view and its bouncing back, it's probably something to do with the layout constraints.
话虽如此,如果您真的需要移动滚动视图并使其弹回,则可能与布局约束有关。
回答by Andrew Ebling
Here's the Official Apple Approved approach: Managing The Keyboard: Text Programming Guide
这是 Apple 官方认可的方法:管理键盘:文本编程指南
However, I would recommend if you are using Autolayout, that you use a constraint to manage the space between the UITextView
and the bottom of the screen and programatically change it's constant value to match that of the height of the keyboard via the keyboard show notification mentioned in the above article and other solutions here. This is because you can receive multiple keyboard show notifications with different heights, which can break if you try and toggle the view frame.
但是,如果您使用自动布局,我建议您使用约束来管理UITextView
屏幕底部和屏幕底部之间的空间,并通过中提到的键盘显示通知以编程方式更改它的常量值以匹配键盘的高度上面的文章和其他解决方案在这里。这是因为您可以收到多个不同高度的键盘显示通知,如果您尝试切换视图框架,这些通知可能会中断。