出现键盘时如何使视图控制器滚动到文本字段

我想让我的uiviewcontroller.xib滚动。 我的视图控制器有8个文本框。 所以我的问题是当我想在第五个textfield写东西,所以我的键盘覆盖文本框。 我怎样才能摆脱这个问题,并使我的viewcontroller滚动?

请详细指导,因为对于iPhone开发来说是新的。

提前致谢。

你可以使用ScrollView 。

添加滚动视图

将一个scrollView拖放到您的视图控制器上,就像使用文本框一样调整尺寸以适应您的需要(您似乎希望它填充视图控制器)。

在这里输入图像说明

然后将文本字段放入滚动视图。 我认为在左边使用文档大纲是最容易的。 将文本字段拖到滚动视图上,如图所示。

在这里输入图像说明

当出现键盘时,使滚动视图滚动

将这段代码添加到viewDidLoad视图控制器中

 //register for keyboard notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; 

并将这些方法添加到您的视图控制器

 // Called when the UIKeyboardDidShowNotification is sent. - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; [self.scrollView setContentOffset:CGPointMake(0, kbSize.height) animated:YES]; } //called when the text field is being edited - (IBAction)textFieldDidBeginEditing:(UITextField *)sender { sender.delegate = self; } 

显示键盘时会调用前两个方法。 第二个是当你开始编辑文本字段时调用的。

现在转到故事板,将文本字段的操作附加到刚刚添加的方法。 您可以右键单击文本字段,select适当的操作并将其拖动到方法。

右键单击文本框时,应该看到类似这样的内容。

在这里输入图像说明

将此属性添加到您的视图控制器,并右键单击从滚动视图拖动到它。 它允许你的视图控制器来控制滚动视图。

 @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; 

喜欢这个:

在这里输入图像说明

closures键盘

当按下返回button时,我们希望键盘closures。

在你的视图控制器头让你的视图控制器UITextFieldDelegate像这样:

 @interface ViewController : UIViewController <UITextFieldDelegate> 

将这段代码添加到viewDidLoad视图控制器中

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

并将这些方法添加到您的视图控制器

 // Called when the UIKeyboardWillHideNotification is sent - (void)keyboardWillBeHidden:(NSNotification*)aNotification { [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES]; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { return [textField resignFirstResponder]; } 

当键盘closures时,第一个方法被调用。 它将滚动视图返回到其原始位置。 第二种方法在编辑完文本字段后调用。 它允许键盘在这种情况下被解雇。

更多信息

以下是有关pipe理键盘的更多信息。

这里的参考是我的ViewController.h

 #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> @end 

和ViewController.m

 #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //register for keyboard notifications [[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; [self.scrollView setContentOffset:CGPointMake(0, kbSize.height) animated:YES]; } // Called when the UIKeyboardWillHideNotification is sent - (void)keyboardWillBeHidden:(NSNotification*)aNotification { [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES]; } - (IBAction)textFieldDidBeginEditing:(UITextField *)sender { sender.delegate = self; } - (BOOL)textFieldShouldReturn:(UITextField *)textField { return [textField resignFirstResponder]; } @end 

所有以前的答案都很好,但如果你不想让自己的代码混淆,使用一个控件。

我喜欢的是: https : //github.com/michaeltyson/TPKeyboardAvoiding (Cocoapods:pod'TPKeyboardAvoiding')

所有你需要做的就是将你的文本字段embedded到UIScrollView(编辑器/embedded式中,首先select你的UITextFields),然后将UIScrollView的类设置为TPKeyboardAvoidingScrollView。

是的,你应该先学习如何手动做,但如果你愿意的话,只要使用它就可以了。

select文本字段并转到编辑器菜单,然后selectembedded – >滚动视图。 这会自动在您的视图层次结构中放置滚动视图,并将文本字段移动到其中。

通常当人们想在屏幕上显示多个文本字段并让它们滚动时,它们实际上将它们放置在一个UITableView中,每个单元格中都有一个文本字段。 通常这是与分组表格视图单元格样式。 如果这样做,表格视图可以在键盘出现时自动调整它的大小,这样你就不必自己处理了。

这是你如何做到的:

 @implementation mainViewController{ CGPoint textFieldPoint; UITextField *curentlyBeingEditingTextField; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _myTextField.delegate=self; textFieldPoint=CGPointMake(160, 228); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (void)keyboardWillShow:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; double delay = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect kbFrame=[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]; [UIView animateWithDuration:0.4f delay:delay-0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{ curentlyBeingEditingTextField.center=CGPointMake(curentlyBeingEditingTextField.center.x,kbFrame.origin.y- kbFrame.size.height-curentlyBeingEditingTextField.frame.size.height); }completion:nil]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ [curentlyBeingEditingTextField resignFirstResponder]; [UIView animateWithDuration:0.4f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ curentlyBeingEditingTextField.center=textFieldPoint; }completion:nil]; } #pragma mark TextFieldDelegate - (void)textFieldDidBeginEditing:(UITextField *)textField{ curentlyBeingEditingTextField=textField; }