iOS Monitor keyboard height changes

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)

Keyboard notifications
Notification (location and size) of the keyboard when the frame changes
Uikeyboardwillchangeframenotification
Uikeyboarddidchangeframenotification
Notifications when the keyboard is displayed
Uikeyboardwillshownotification
Uikeyboarddidshownotification
Notifications issued when the keyboard is hidden
Uikeyboardwillhidenotification
Uikeyboarddidhidenotification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];//在这里注册通知

Here is the Listener notification:

Pragma mark-monitoring method

/**

  • Called when the frame of the keyboard changes (show, hide, etc.)
    */
  • (void) Keyboardwillchangeframe: (nsnotification *) notification
    {
    if (self.picking) return;
    /**
    Notification.userinfo = @{
    Keyboard popup \ Hidden frame
    Uikeyboardframeenduserinfokey = Nsrect: {{0, 352}, {320, 216}},
    Keyboard pop-up \ Hide Time spent
    Uikeyboardanimationdurationuserinfokey = 0.25,
    Keyboard popup \ Hide Animation execution Rhythm (first fast, slow, constant)
    Uikeyboardanimationcurveuserinfokey = 7
    }
    */

    Nsdictionary *userinfo = Notification.userinfo;

    Duration of animation
    Doubleduration = [Userinfo[uikeyboardanimationdurationuserinfokey] doublevalue];

    The frame of the keyboard
    CGRect KEYBOARDF = [Userinfo[uikeyboardframeenduserinfokey] cgrectvalue];

    Performing animations
    [UIView animatewithduration:duration animations:^{
    Y value of the toolbar = = y value of the keyboard-height of the tool bar
    The Y value of the IF (Keyboardf.origin.y > Self.view.height) {//keyboard has far exceeded the height of the controller view
    Self.toolbar.y = self.view.height-self.toolbar.height;//The self.toolbar here is my input box.

    }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

@interfaceUIView (Extension)
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) cgfloat width;
@property (nonatomic, assign) cgfloat height;
@property (nonatomic, assign) CGFloat CenterX;
@property (nonatomic, assign) CGFloat centery;
@property (nonatomic, assign) cgsize size;
@property (nonatomic, assign) Cgpoint origin;
@end
Implementations in. m files (overriding setter and getter)

@implementationUIView (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
    {
    returnself.frame.origin.x;
    }

  • (cgfloat) Y
    {
    RETURNSELF.FRAME.ORIGIN.Y;
    }

  • (void) Setcenterx: (cgfloat) CenterX
    {
    Cgpoint Center = self.center;
    center.x = CenterX;
    Self.center = center;
    }

  • (cgfloat) CenterX
    {
    returnself.center.x;
    }

  • (void) Setcentery: (cgfloat) CenterY
    {
    Cgpoint Center = self.center;
    Center.y = CenterY;
    Self.center = center;
    }

  • (cgfloat) CenterY
    {
    RETURNSELF.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
    {
    Returnself.frame.size.height;
    }

  • (cgfloat) Width
    {
    Returnself.frame.size.width;
    }

  • (void) SetSize: (cgsize) size
    {
    CGRect frame = self.frame;
    frame.size = size;
    Self.frame = frame;
    }

  • (cgsize) Size
    {
    Returnself.frame.size;
    }

  • (void) Setorigin: (Cgpoint) origin
    {
    CGRect frame = self.frame;
    Frame.origin = origin;
    Self.frame = frame;
    }

  • (Cgpoint) Origin
    {
    Returnself.frame.origin;
    }
    @end
    A friend in need can use it directly.

iOS Monitor keyboard height changes

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.