IOS development-get the ios keyboard height

Source: Internet
Author: User

IOS development-get the ios keyboard height

 
-(Void) viewDidLoad {[super viewDidLoad]; // adds a listener to receive messages when the keyboard appears or changes
 

[[Nsicationcenter center defacenter center] addObserver: self

Selector: @ selector (keyboardDidShow :)

Name: UIKeyboardWillShowNotification

Object: nil];

// Adds a listener to receive messages when the key exits.
 

[[Nsicationcenter center defacenter center] addObserver: self

Selector: @ selector (keyboardDidHide :)

Name: UIKeyboardWillHideNotification

Object: nil];

}
// Called when the keyboard appears or changes

-(Void) keyboardDidShow :( NSNotification *) notification {

NSDictionary * userInfo = [notification userInfo];

CGSize keyboardSize = [[userInfo objectForKey: UIKeyboardFrameBeginUserInfoKey] CGRectValue]. size;

_ KeyBoardHeight = keyboardSize. height;

[Self changeViewYByShow];

}

 

 
// Called when the keyboard is hidden
 
 

-(Void) keyboardDidHide :( NSNotification *) notification {

_ KeyBoardHeight = 0;

[Self changeViewYByHide];

}


 
 

# Pragma mark-private methods

-(Void) changeViewYByShow {

[UIView animateWithDuration: 0.2 animations: ^ {

CGRect rect = self. view. frame;

Rect. origin. y-= self. keyBoardHeight;

Self. view. frame = rect;

}];

}

 

-(Void) changeViewYByHide {

CGRect rect = self. view. frame;

Rect. origin. y = 64;

Self. view. frame = rect;

}

 

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.