Monitor keyboard height changes for iOS development

Source: Internet
Author: User

Recent projects, there is a similar to the comment forwarding function, the bottom of the screen has an input box to do with TextView, when TextView becomes the first responder, its Y value changes with the height of the keyboard, to ensure that the TextView close to the keyboard, but will not be blocked by the keyboard.

Here's how I implemented it: (using notifications)

    Notification of keyboard notification//keyboard changes when frame changed (location and size)    //    uikeyboardwillchangeframenotification    //    Uikeyboarddidchangeframenotification    //    uikeyboardwillshownotification    /    /// Uikeyboarddidshownotification    //When the keyboard is hidden///    uikeyboardwillhidenotification    // Uikeyboarddidhidenotification        [[nsnotificationcenter defaultcenter] addobserver:self selector: @selector ( Keyboardwillchangeframe:) name:uikeyboardwillchangeframenotification object:nil];//Registration notice here

Here is the Listener notification:

#pragma mark-Listener method/** * Call (show, hide, etc.) */-(void) Keyboardwillchangeframe when the frame of the keyboard changes (Nsnotification *) notification{//    if (self.picking) return; /** Notification.userinfo = @{//keyboard popup \ Hidden Frame Uikeyboardframeenduserinfokey = Nsrect: {{0, 352}, {320, 216} },//keyboard pop-up \ Hide the time Uikeyboardanimationdurationuserinfokey = 0.25,//keyboard popup \ Hide Animation execution Rhythm (first fast after slow, constant speed) Uikeyboardan        Imationcurveuserinfokey = 7} */Nsdictionary *userinfo = Notification.userinfo;        Duration of the animation double duration = [Userinfo[uikeyboardanimationdurationuserinfokey] doublevalue];        Keyboard frame CGRect KEYBOARDF = [Userinfo[uikeyboardframeenduserinfokey] cgrectvalue]; Perform animation [UIView animatewithduration:duration animations:^{//Toolbar Y value = = Keyboard Y value-the height of the toolbar if (Keyboardf.orig IN.Y > Self.view.height) The y value of the {//keyboard has far exceeded the height of the controller view SELF.TOOLBAR.Y = Self.view.height-self.toolbar.height ;//Here's <span style= "Background-color:rgb (240, 240, 240);" >self.toolbar is my input box.        </span>} else {self.toolbar.y = Keyboardf.origin.y-self.toolbar.height; }    }];}


Of course, here I wrote a category for UIView, implemented as follows:

declarations in. h files

@interface UIView (Extension) @property (nonatomic, assign) CGFloat x; @property (nonatomic, assign) cgfloat y; @property (n Onatomic, assign) cgfloat width; @property (nonatomic, assign) cgfloat height; @property (nonatomic, assign) CGFloat Center X; @property (nonatomic, assign) CGFloat centery; @property (nonatomic, assign) cgsize size; @property (nonatomic, assign) C Gpoint origin; @end
Implementations in. m files (overriding setter and getter)

@implementation UIView (Extension)-(void) SetX: (cgfloat) x{cgrect frame = self.frame;    frame.origin.x = x; Self.frame = frame;}    -(void) Sety: (cgfloat) y{cgrect frame = self.frame;    FRAME.ORIGIN.Y = y; Self.frame = frame;} -(CGFloat) x{return self.frame.origin.x;} -(CGFloat) y{return SELF.FRAME.ORIGIN.Y;}    -(void) Setcenterx: (cgfloat) centerx{Cgpoint Center = self.center;    center.x = CenterX; Self.center = center;} -(CGFloat) centerx{return self.center.x;}    -(void) Setcentery: (cgfloat) centery{Cgpoint Center = self.center;    Center.y = CenterY; Self.center = center;} -(CGFloat) centery{return self.center.y;}    -(void) SetWidth: (cgfloat) width{cgrect frame = self.frame;    Frame.size.width = width; Self.frame = frame;}    -(void) SetHeight: (cgfloat) height{cgrect frame = self.frame;    Frame.size.height = height; Self.frame = frame;} -(CGFloat) height{return self.frame.size.height;} -(CGFloat) width{return self.frame.size.width;} -(void) SETsize: (cgsize) size{cgrect frame = self.frame;    frame.size = size; Self.frame = frame;} -(cgsize) size{return self.frame.size;}    -(void) Setorigin: (cgpoint) origin{cgrect frame = self.frame;    Frame.origin = origin; Self.frame = frame;} -(Cgpoint) origin{return self.frame.origin;} @end
Those in need can use them directly.


Monitor keyboard height changes for iOS development

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.