IOS TextField Companion Keyboard move

Source: Internet
Author: User

This article describes a simple and practical small method.

I think some of the effects of landing should not be unfamiliar to everyone.

Let's introduce the implementation of the keyboard pop-up TextField accompanying the keyboard movement when you start typing TextField text.

Take a look at the demo effect first

We are bound by the Textfiel. The constraint content is as follows??

After the constraint is over, we need to do a very important thing is to drag the bottom constraint of the TextField to the appropriate code area.

The content is as follows??

By doing this, we can implement the content of the response through the code.

The implementation code is as follows:

viewcontroller.m//CX textfiled Implementation of keyboard movement////Created by Ma C on 16/3/31.//Copyright? 2016 Xubaoaichiyu. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () @property (weak, nonatomic) Iboutlet    Nslayoutconstraint *bottomspace; @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload]; CV notification [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector ( Keyboardwillchangeframenotification:) name:uikeyboardwillchangeframenotification Object:nil];} -(void) Keyboardwillchangeframenotification: (nsnotification *) note{//Get keyboard hungry frame cgrect frmae = [Note.userinfo[uikey        Boardframeenduserinfokey] Cgrectvalue];    Let textfiled the bottom of the screen height minus the top of the keyboard Y value can//note here do not make it equal to the height of the keyboard, because the height of the dead, will cause the keyboard down, the TextField did not go down the results.        Self.bottomSpace.constant = [UIScreen mainscreen].bounds.size.height-frmae.origin.y; Gets the animation time of the keyboard so that the TextField is consistent with the shape of the keyboard cgfloat interval = [Note.userinfo[uikeyboardanimationdurationuserinfokey] Doublevalue]; Animate the text [UIView animatewithduration:interval animations:^{//Note here is not a change in value, the value has been changed before,//what needs to be done here is to force the cloth            Bureau [Self.view Layoutifneeded];    }];    }-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{[Self.view Endediting:yes]; } @end

IOS TextField Companion Keyboard move

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.