加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 01:50:20 所属栏目:百科 来源:网络整理
导读:我想让我的uiviewcontroller.xib滚动.我的视图控制器有8个文本字段.所以我的问题是当我想在第五个文本字段中写东西时,我的键盘上的内容覆盖了文本字段.如何摆脱这个问题,让我的viewcontroller滚动? 请详细说明,因为我是iPhone开发的新手. 提前致谢. 解决方
我想让我的uiviewcontroller.xib滚动.我的视图控制器有8个文本字段.所以我的问题是当我想在第五个文本字段中写东西时,我的键盘上的内容覆盖了文本字段.如何摆脱这个问题,让我的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;
}

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

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

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

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

@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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读