# # IOS with the AutoLayout occlusion keyboard Processing # #

Source: Internet
Author: User
Tags notification center
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];
 }
Related Article

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.