您可以使用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;
}
这些方法中的前两个在显示键盘时被调用。第二个在您开始编辑文本字段时调用。
现在转到您的故事板并将文本字段的操作附加到刚刚添加的方法。您可以右键单击文本字段,选择适当的操作并将其拖到方法中。
当您右键单击文本字段时,您应该会看到类似的内容。
将此属性添加到您的视图控制器并右键单击从滚动视图拖动到它。它允许您的视图控制器控制滚动视图。
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
像这样:
关闭键盘
当按下返回按钮时,我们希望键盘关闭。
在您的视图控制器标题中将您的视图控制器设为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];
}
第一个方法在键盘关闭时调用。它将滚动视图返回到其原始位置。完成编辑文本字段后调用第二种方法。它允许在发生这种情况时关闭键盘。
更多信息
Here 是有关管理键盘的更多信息。
这里是我的 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