[英]How to make the view controller scroll to text field when keyboard appears
I want to make my uiviewcontroller.xib
scroll. 我想让我的
uiviewcontroller.xib
滚动。 My view controller has like 8 textfields. 我的视图控制器有8个文本字段。 So my problem is when I want to write something in the 5th
textfield
and so on my keyboard covers the textfields. 所以我的问题是当我想在第五个
textfield
写东西时,我的键盘上的内容覆盖了文本字段。 How can I get rid of this problem, and make my viewcontroller scroll? 如何摆脱这个问题,让我的viewcontroller滚动?
Please guide in detail because am new to iPhone development. 请详细说明,因为我是iPhone开发的新手。
Thanks in advance. 提前致谢。
You can use a ScrollView . 您可以使用ScrollView 。
Adding the scroll view 添加滚动视图
Drag an drop a scrollView onto your view controller, the same way you would with a text field and adjust the dimensions to suit your needs (it seems like you'd want it to fill the view controller.) 将dropView拖放到视图控制器上,就像使用文本字段一样,并调整尺寸以满足您的需要(看起来您希望它填充视图控制器。)
Then place the text fields into the scroll view. 然后将文本字段放入滚动视图。 I think it's easiest to do using the document outline on the left.
我认为使用左侧的文档大纲最简单。 Drag the text fields onto the scroll view here, like in the picture.
将文本字段拖到滚动视图上,如图所示。
Making the scroll view scroll when keyboard appears 键盘出现时滚动视图滚动
Add this code to your view controller in viewDidLoad
将此代码添加到
viewDidLoad
视图控制器
//register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
And add these methods to your view controller 并将这些方法添加到视图控制器
// 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;
}
The first two of these methods is called when the keyboard is shown. 显示键盘时会调用前两个方法。 The second is called when you start to edit a text field.
当您开始编辑文本字段时,将调用第二个。
Now go to your storyboard and attach actions of the text fields to the method that was just added. 现在转到您的故事板并将文本字段的操作附加到刚刚添加的方法。 You can right click on the text field, select the appropriate action and drag it to the method.
您可以右键单击文本字段,选择相应的操作并将其拖到方法中。
Your should see something like this when you right click on your textfields. 当您右键单击文本字段时,您应该看到类似的内容。
Add this property to your view controller and right click drag from your scroll view to it. 将此属性添加到视图控制器,然后右键单击从滚动视图拖动到该视图。 It allows your view controller to control the scroll view.
它允许您的视图控制器控制滚动视图。
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
Like this: 像这样:
Closing the keyboard 关闭键盘
When the return button is pressed we want the keyboard to close. 按下返回按钮后,我们希望键盘关闭。
In your view controller header make your view controller a UITextFieldDelegate
Like this: 在您的视图控制器标题中,使您的视图控制器成为
UITextFieldDelegate
如下所示:
@interface ViewController : UIViewController <UITextFieldDelegate>
Add this code to your view controller in viewDidLoad
将此代码添加到
viewDidLoad
视图控制器
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
And add these methods to your view controller 并将这些方法添加到视图控制器
// 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];
}
The first method is called when the keyboard is closed. 键盘关闭时调用第一种方法。 It returns the scroll view to its original position.
它将滚动视图返回到其原始位置。 The second method is called when you have finished editing a text field.
编辑完文本字段后,将调用第二种方法。 It allows the keyboard to be dismissed when this happens.
它允许在发生这种情况时解除键盘。
More info 更多信息
Here is more information on managing the keyboard. 以下是有关管理键盘的更多信息。
And for reference here is my ViewController.h 这里是我的ViewController.h供参考
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
@end
and ViewController.m 和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
All the previous answers are great, but if you don't want to confuse yourself with code, use a control. 以前的所有答案都很棒,但如果您不想将自己与代码混淆,请使用控件。
One that I love is: https://github.com/michaeltyson/TPKeyboardAvoiding (Cocoapods: pod 'TPKeyboardAvoiding') 我喜欢的是: https : //github.com/michaeltyson/TPKeyboardAvoiding (Cocoapods:pod'TPKeyboardAvoiding')
All you have to do is embed your text fields into a UIScrollView (Editor/Embed In. Select your UITextFields first) then set that UIScrollView's class to TPKeyboardAvoidingScrollView. 您所要做的就是将文本字段嵌入UIScrollView(编辑/嵌入。首先选择您的UITextFields),然后将UIScrollView的类设置为TPKeyboardAvoidingScrollView。
Yes you should learn how to do it manually first, but after just use this if you'd like. 是的,您应该首先学习如何手动完成,但是如果您愿意,可以使用它。
Select the text fields and go to the Editor menu, and choose Embed In -> Scroll View. 选择文本字段并转到“编辑器”菜单,然后选择“嵌入” - >“滚动视图”。 This will automatically place a scroll view in your view hierarchy and move the text fields into it.
这将自动在视图层次结构中放置滚动视图,并将文本字段移动到其中。
Also often when people want to have multiple text fields on screen and have them scroll, they will actually place them in a UITableView, where each cell has a text field in it. 此外,当人们想要在屏幕上显示多个文本字段并让它们滚动时,它们实际上会将它们放在UITableView中,其中每个单元格中都有一个文本字段。 Usually this is with the grouped table view cell style.
通常这是分组表视图单元格样式。 If you do it this way, the table view can handle automatically adjusting its size when the keyboard appears, so that you don't have to handle this yourself.
如果这样做,表格视图可以在键盘出现时自动调整其大小,这样您就不必自己处理。
This is how you do it: 这是你如何做到的:
@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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.