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

Exc*_*ion 9 xcode objective-c uiscrollview uiviewcontroller ios

我想制作我的uiviewcontroller.xib卷轴.我的视图控制器有8个文本字段.所以我的问题是当我想在第5页写东西时textfield,我的键盘上的文字区域也是如此.如何摆脱这个问题,让我的viewcontroller滚动?

请详细说明,因为我是iPhone开发的新手.

提前致谢.

Con*_*nor 16

您可以使用ScrollView.

添加滚动视图

将dropView拖放到视图控制器上,就像使用文本字段一样,并调整尺寸以满足您的需要(看起来您希望它填充视图控制器.)

在此输入图像描述

然后将文本字段放入滚动视图.我认为使用左侧的文档大纲最简单.将文本字段拖到滚动视图上,如图所示.

在此输入图像描述

键盘出现时滚动视图滚动

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

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

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

// 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;
}
Run Code Online (Sandbox Code Playgroud)

当显示键盘时,将调用前两个方法.当您开始编辑文本字段时,将调用第二个.

现在转到您的故事板并将文本字段的操作附加到刚刚添加的方法.您可以右键单击文本字段,选择相应的操作并将其拖到方法中.

当您右键单击文本字段时,您应该看到类似的内容.

在此输入图像描述

将此属性添加到视图控制器,然后右键单击从滚动视图拖动到该视图.它允许您的视图控制器控制滚动视图.

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
Run Code Online (Sandbox Code Playgroud)

像这样:

在此输入图像描述

关闭键盘

按下返回按钮后,我们希望键盘关闭.

在您的视图控制器标题中,使您的视图控制器UITextFieldDelegate 像这样:

@interface ViewController : UIViewController <UITextFieldDelegate>
Run Code Online (Sandbox Code Playgroud)

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

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillBeHidden:)
                                             name:UIKeyboardWillHideNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

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

// 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];
}
Run Code Online (Sandbox Code Playgroud)

键盘关闭时调用第一种方法.它将滚动视图返回到其原始位置.编辑完文本字段后,将调用第二种方法.它允许在发生这种情况时解除键盘.

更多信息

以下是有关管理键盘的更多信息.

这里是我的ViewController.h供参考

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>

@end
Run Code Online (Sandbox Code Playgroud)

和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
Run Code Online (Sandbox Code Playgroud)


Nat*_*Lee 5

以前的所有答案都很棒,但如果您不想将自己与代码混淆,请使用控件.

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

您所要做的就是将文本字段嵌入UIScrollView(编辑/嵌入.首先选择您的UITextFields),然后将UIScrollView的类设置为TPKeyboardAvoidingScrollView.

是的,您应该首先学习如何手动完成,但是如果您愿意,可以使用它.