
请详细说明,因为我是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 总结 以上是内存溢出为你收集整理的ios – 如何在键盘出现时使视图控制器滚动到文本字段全部内容,希望文章能够帮你解决ios – 如何在键盘出现时使视图控制器滚动到文本字段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)