[Code Note] iOS-only allows textField to use keyboard notifications, ios-textfield

Source: Internet
Author: User

[Code Note] iOS-only allows textField to use keyboard notifications, ios-textfield

Code:

# Import "ViewController. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // Add a keyboard event for textField [[nsnotifcenter center defacenter center] addObserver: self selector: @ selector (addKeyboardNoti) name: jsonobject: nil]; [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (removeKeyboardNoti) name: UITextFieldTextDidEndEditingNotification object: nil] ;}# pragma-mark-keyboard javasicatin // keyboard event-(void) keyboardWillShow :( NSNotification *) notification {NSDictionary * info = [notification userInfo]; // The keyboardHeight is the keyboard height CGSize keyboardSize = [[info objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue]. size; [self animateViewWithKeyboardHeight: keyboardSize. height];}-(void) keyboardWillHide :( NSNotification *) notification {[self defined: 0.0];}-(void) Updated :( CGFloat) keyboardHeight {NSTimeInterval animationDuration = 0.3f; CGFloat height = self. view. bounds. size. height; CGFloat width = self. view. bounds. size. width; CGFloat topSize = 0.0; CGFloat viewH = self. view. frame. size. height-64; CGFloat deviceHeight = [UIScreen mainScreen]. bounds. size. height; CGFloat animateH = deviceHeight-viewH-keyboardHeight; if (animateH> = 0) {topSize = 0; CGRect toRect = CGRectMake (0, topSize, width, height); self. view. frame = toRect;} else {topSize = animateH; CGRect toRect = CGRectMake (0, topSize, width, height); [UIView animateWithDuration: animationDuration animations: ^ {self. view. frame = toRect ;}] ;}# pragma-mark-UITextFieldText Notification // Add a keyboard event-(void) addKeyboardNoti {NSLog (@ "------ addKeyboardNoti -------"); [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardWillShow :) name: jsonobject: nil]; [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (handler :) name: jsonobject: nil] ;}// remove Keyboard Events-(void) removeKeyboardNoti {NSLog (@ "------ removeKeyboardNoti ---------"); [[nsicationicationcenter defacenter center] removeObserver: self name: UIKeyboardWillShowNotification object: nil]; [[nsicationcenter center defacenter center] removeObserver: self name: icationobject: nil];}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end

 

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.