UI keyboard notification and ui keyboard

Source: Internet
Author: User
Tags notification center

UI keyboard notification and ui keyboard

 

 

# Import "ViewController. h"

 

@ Interface ViewController ()

@ Property (nonatomic, strong) UITextField * tf;

@ End

 

@ Implementation ViewController

 

-(Void) viewDidLoad {

[Super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

_ Tf = [[UITextField alloc] initWithFrame: CGRectMake (10, self. view. bounds. size. height-40,300, 30)];

_ Tf. borderStyle = UITextBorderStyleRoundedRect;

[Self. view addSubview: _ tf];

/*

Notification center (Singleton)

The notification center is one-to-multiple, that is, the same broadcast can be received by multiple radios.

Proxy is one-to-one

Purpose: 1. It is used to receive and initiate broadcasts.

2. Use the notification name as the channel

*/

/*

Parameter 1: Response class

Parameter 2: response methods in the class

Parameter 3: Notification name (Channel)

Parameter 4: receiving type [note] nil represents any type

*/

//: UIKeyboardWillShowNotification receives the notification to be displayed on the keyboard

[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyBoardWillShow :) name: UIKeyboardWillShowNotification object: nil];

// UIKeyboardWillHideNotification receives notifications to be hidden on the keyboard

[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyBoardWillHide :) name: UIKeyboardWillHideNotification object: nil];

}

# Pragma mark-UIKeyboardWillHideNotification

// Hide the keyboard

-(Void) keyBoardWillHide :( NSNotification *) noti

{

// Noti. userInfo is a dictionary. You can output it to see what the dictionary contains.

// NSLog (@ "% @", noti. userInfo );

 

// Obtain the coordinates after the bullet. [Note that the coordinates are after the bullet.]

CGRect keyBoardEndFrame = [[noti. userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];

// NSLog (@ "-- % @", NSStringFromCGRect (keyBoardEndFrame ));

[UIView animateWithDuration: 2 animations: ^ {

// Change the position of the text box and let it pop up with the keyboard

CGRect tfRext = _ tf. frame;

TfRext. origin. y = keyBoardEndFrame. origin. y-_ tf. bounds. size. height-10;

_ Tf. frame = tfRext;

}];

}

 

# Pragma mark-UIKeyBoard notification

// The notification is received, so NSNotification is used for the parameter.

// The keyboard will be displayed

-(Void) keyBoardWillShow :( NSNotification *) noti

{

CGRect keyBoardFrame = [[noti. userInfo objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];

// You can output CGRect using the following methods:

// NSStringFromCGRect convert CGRect into a string to obtain the Frame

// NSLog (@ "% @", NSStringFromCGRect (keyBoardFrame ));

// Obtain the animation duration of the keyboard

// CGFloat keyBoardDuration = [noti. userInfo [UIKeyboardAnimationDurationUserInfoKey] floatValue];

[UIView animateWithDuration: 2 animations: ^ {

CGRect tfRect = _ tf. frame;

TfRect. origin. y = keyBoardFrame. origin. y-_ tf. bounds. size. height-10;

_ Tf. frame = tfRect;

}];

}

# Pragma mark-called when your fingers touch any part of the screen

-(Void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event

{

// Hide the keyboard with end editing

[Self. view endEditing: YES];

}

 

 

@ 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.