1. Initialize input box, listen input box start Edit and End edit
//Password entry boxUitextfield *logininputpasswordstr = [Uitextfield inputtextwithimage:[UIImageimagenamed:@"Password"] Placeholdertext:nslocalizedstring (@"Password",Nil)]; Logininputpasswordstr. Frame= Rectmakewithpercent ( -/375.0,385/667.0,327/375.0, -/667.0);//Set Keyboard typeLogininputpasswordstr. Keyboardtype= Uikeyboardtypedefault;//Set Dark text[Logininputpasswordstr setsecuretextentry:YES];//Listen password input box text start input[Logininputpasswordstr AddTarget: SelfAction@selector(Logininputpasswordstrinputbegin) Forcontrolevents:uicontroleventeditingdidbegin];//Listen password input box text end input[Logininputpasswordstr AddTarget: SelfAction@selector(Logininputpasswordstrinputend) forcontrolevents:uicontroleventeditingdidend]; [ Self. ViewADDSUBVIEW:LOGININPUTPASSWORDSTR];
2. When you start editing, the view moves up, and the initial position is restored when you finish editing or tapping the screen.
//loginInputPasswordStr监听方法-(void)loginInputPasswordStrInputBegin{ [self moveView];}-(void)loginInputPasswordStrInputEnd{ [self resumeView];}// 点击屏幕取消输入,恢复视图初始位置-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES]; [self resumeView];}
3. Body Method!
#pragma mark-click the input box view to move up/restore overallRestore original view location-(void) resumeview{NSLog (@"restore original View location before SELF.VIEW.ORIGIN.Y:%f", self. View. Origin. Y);When the view moves up thePixels-if (self. View. Frame. Origin. Y== - the) {Nstimeinterval animationduration=0.40F;[UIView beginanimations:@"Resizeforkeyboard"Context:nil];[UIView Setanimationduration:animationduration];If the current view is a parent view, theYFor -Pixel height, which is dynamically adjusted if the current view is a child of another viewYThe height of float FX = self. View. Frame. Origin. x;float FY = self. View. Frame. Origin. Y+ the;float width = self. View. Frame. Size. Width;float height = self. View. Frame. Size. Height;Move Down theUnits, set self according to the actual situation. View. Frame= CGRectMake (FX, FY, width, height);[UIView Commitanimations];}}//Mobile Window-(void) moveview{NSLog (@"self.view.origin.y before moving window:%f", self. View. Origin. Y);If the view is in its initial position, the IF (self. View. Origin. Y==0) {Nstimeinterval animationduration=0.40F;[UIView beginanimations:@"Resizeforkeyboard"Context:nil];[UIView Setanimationduration:animationduration];float FX = self. View. Frame. Origin. x;float FY = self. View. Frame. Origin. Y- the;float width = self. View. Frame. Size. Width;float height = self. View. Frame. Size. Height;Move Up theA unit self. View. Frame= CGRectMake (FX, FY, width, height);[UIView Commitanimations];}}
ios-Monitor keyboard input, view move up or restore overall-avoid input occlusion