xcode 出现键盘时如何使视图控制器滚动到文本字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20909673/
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 make the view controller scroll to text field when keyboard appears
提问by Exception
I want to make my uiviewcontroller.xib
scroll. My view controller has like 8 textfields. So my problem is when I want to write something in the 5th textfield
and so on my keyboard covers the textfields. How can I get rid of this problem, and make my viewcontroller scroll?
我想做我的uiviewcontroller.xib
卷轴。我的视图控制器有 8 个文本字段。所以我的问题是当我想在第 5 位写一些东西时textfield
,我的键盘会覆盖文本字段。我怎样才能摆脱这个问题,并使我的视图控制器滚动?
Please guide in detail because am new to iPhone development.
请详细指导,因为我是 iPhone 开发的新手。
Thanks in advance.
提前致谢。
回答by Connor
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.)
将一个 scrollView 拖放到您的视图控制器上,就像使用文本字段一样,然后调整尺寸以满足您的需要(似乎您希望它填充视图控制器。)
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
更多信息
Hereis 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
回答by Nate Lee
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.
是的,您应该先学习如何手动执行此操作,但是如果您愿意,可以在使用它之后。
回答by Gavin
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. 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.
通常当人们想要在屏幕上有多个文本字段并让它们滚动时,他们实际上会将它们放在 UITableView 中,其中每个单元格都有一个文本字段。通常这是使用分组表视图单元格样式。如果你这样做,当键盘出现时,表格视图可以自动调整其大小,这样你就不必自己处理了。
回答by Dasem
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;
}