IOS uses a AutoLayout occlusion keyboard for processing
Explain my situation first: 1. I used a simple login problem with the combination of masonry and iOS AutoLayout.
LoginView is one of my login background,, set the center, when I click Input, I will determine whether the keyboard will block my LoginView, if not blocked, I do not need to change the LoginView frame,,, in AutoLayout, If you want to get the correct frame must be in the Viewdidappear method, in short, after the Viewdidappear method can be.
-(void) viewdidload {[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (
Keyboardwillshownotification:) name:uikeyboardwillshownotification Object:nil]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillhidenotification:) Name:
Uikeyboardwillhidenotification Object:nil]; #pragma mark-notification Center-(void) Keyboardwillshownotification: (nsnotification *) Notification {//Get keyboard base Letter
(animation length and keyboard height) nsdictionary *userinfo = [notification UserInfo];
NSLog (@ "log:%@", Nsstringfromcgrect (Self.loginView.frame));
NSLog (@ "%@", userInfo);
CGRect rect = [Userinfo[uikeyboardframebeginuserinfokey] cgrectvalue]; CGFloat keyboardheight = cgrectgetheight (rect)-(Cgrectgetheight (Self.view.frame)-
Self.loginview.frame.origin.y-cgrectgetheight (Self.loginView.frame));
CGFloat keyboardduration = [Userinfo[uikeyboardanimationdurationuserinfokey] doublevalue];
Determine if the view is blocked by the keyboard if (keyboardheight>0) { Modify margin constraints [Self.loginview mas_updateconstraints:^ (Masconstraintmaker *make) {Make.centerY.mas_e
Qualto (Self.view). Offset (-keyboardheight);
}];
Update constraint [UIView animatewithduration:keyboardduration animations:^{[Self.view layoutifneeded];
}]; }-(void) Keyboardwillhidenotification: (nsnotification *) Notification {//Get keyboard animation length nsdictionary *userinfo = [n
Otification UserInfo];
CGFloat keyboardduration = [Userinfo[uikeyboardanimationdurationuserinfokey] doublevalue]; Modify to Previous constraint (0 from bottom) [Self.loginview mas_updateconstraints:^ (Masconstraintmaker *make) {Make.center.mas_equalTo
(Self.view);
}];
Update constraint [UIView animatewithduration:keyboardduration animations:^{[Self.view layoutifneeded];
}];
}-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event {[Super Touchesbegan:touches withevent:event];
[Self.view Endediting:yes];
}-(void) Dealloc { [[Nsnotificationcenter Defaultcenter] removeobserver:self name:uikeyboardwillchangeframenotification Object:nil];
}