韩国韩荣部品株式会社,杨益平,78能去海山吗
当我们在uitextfield输入数据时经常弹出键盘遮挡界面,解决方法是:在弹出键盘时将整个uiview向上移动,在键盘消失时,uiview还原。
实例代码如下:
@interface viewcontroller ()<uitextfielddelegate> @property(nonatomic,strong)uitextfield* tf; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; self.tf = [[uitextfield alloc]initwithframe:cgrectmake(10, 600, 100, 20)]; self.tf.delegate = self; self.tf.backgroundcolor = [uicolor blackcolor]; [self.view addsubview:self.tf]; } #pragma mark life circle -(void)viewwillappear:(bool)animated{ [super viewwillappear:animated]; //后台切换到前台通知 [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(applicationwillenterforeground)name:uiapplicationwillenterforegroundnotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillhide:) name:uikeyboardwillhidenotification object:nil]; [self.view endediting:yes]; } - (void)applicationwillenterforeground{ [self.view endediting:yes]; } -(void)touchesended:(nsset<uitouch *> *)touches withevent:(uievent *)event{ [self.view endediting:yes]; } #pragma mark uitextfielddelegate -(void)textfielddidbeginediting:(uitextfield *)textfield{ //第一个cell不往上弹输入框的位置 // if(indexpath.row!=0){ [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboardwillchange:) name:uikeyboardwillchangeframenotification object:nil]; // } } -(void)textfielddidendediting:(uitextfield *)textfield{ } #pragma mark 键盘操作 - (void)keyboardwillchange:(nsnotification *)note { nsdictionary *userinfo = note.userinfo; cgfloat duration = [userinfo[@"uikeyboardanimationdurationuserinfokey"] doublevalue]; cgrect keyframe = [userinfo[@"uikeyboardframeenduserinfokey"] cgrectvalue]; //这个64是我减去的navigationbar加上状态栏20的高度,可以看自己的实际情况决定是否减去; cgfloat movey = keyframe.origin.y -self.tf.frame.origin.y-self.tf.frame.size.height; nslog(@"%f",movey); [uiview animatewithduration:duration animations:^{ self.view.transform = cgaffinetransformmaketranslation(0, movey); }]; } - (void)keyboardwillhide:(nsnotification *)nsnotification { [[nsnotificationcenter defaultcenter]removeobserver:self name:uikeyboardwillchangeframenotification object:nil]; [uiview animatewithduration:0.2 animations:^{ self.view.transform = cgaffinetransformmaketranslation(0, 0); }]; } @end
以上这篇ios 防键盘遮挡的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
网友评论