ios-Monitor keyboard input, view move up or restore overall-avoid input occlusion

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.