键盘出现时如何向上移动UIScrollView?

时间:2014-12-17 08:56:55

标签: ios objective-c xcode uiscrollview keyboard

更新

我的问题是:如何在单击另一个文本字段时阻止将scrollview重置为该位置?

实际上我已经尝试了几种方法,例如解决方案question

最初,我的scrollView(其中包含多个textfield和textview,以及所有位置都放在storyboard中)位于(0,302)位置。当键盘出现时,我想将其向上移动到(0,100)。首先,我尝试添加一个按钮并执行以下操作,它可以正常工作。

CGRect frame = self.informationScrollView.frame;
frame.origin.y = 100;
self.informationScrollView.frame = frame;

然后我尝试在UIKeyboardDidShowNotification的选择器中执行相同的代码,但是失败了。我发现即使活动textField发生了变化,该位置也会被重置。任何人都可以告诉我如何防止该应用程序重置该职位?非常感谢。

6 个答案:

答案 0 :(得分:3)

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];

对于多个TextFields:

How to make a UITextField move up when keyboard is present?

希望这有帮助。

答案 1 :(得分:3)

如果ypu正在使用UIScrollView,那么你应该看看TPAvoidingScrollView。它易于使用并给出预期的结果。您只需将TPAvoidingScrollView设置为UIScrollView的超类,TPScrollView将处理所有内容。就试一试吧。希望这有帮助。

答案 2 :(得分:1)

非常简单将此用于viewDidLoad方法

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

并为其中的键盘高度编写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;
}

它有效。

答案 3 :(得分:1)

使用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];

使用本地方法处理:

-(void)keyboardWasShown:(NSNotification*)notification
-(void)keyboardWillBeHidden:(NSNotification *)notification

答案 4 :(得分:1)

UIScrollView的最佳操作方法是根据键盘的外观设置 contentInsets scrollIndicatorInsets 。如果滚动视图的内容是您的UITextField和UITextView实例,那么您应该通过 contentOffset 偏移滚动视图内容,而不是移动滚动视图的框架。

话虽这么说,如果你真的需要移动滚动视图并将其弹回,这可能与布局约束有关。

答案 5 :(得分:0)

以下是Apple批准的官方方法:Managing The Keyboard: Text Programming Guide

但是,如果您使用的是Autolayout,我会建议您使用约束来管理UITextView和屏幕底部之间的空间,并以编程方式更改它的常量值以匹配键盘的高度通过键盘显示上述文章中提到的通知和此处的其他解决方案。这是因为您可以接收多个具有不同高度的键盘显示通知,如果您尝试切换视图框架,这可能会中断。

相关问题