IOS development (21) about UITextField keyboard occlusion

Source: Internet
Author: User

1 Preface
During App creation, the UITextField Keyboard will block the following content. Because IOS does not have its own mechanism, you need to write your own method to control it, today, we will introduce a simple method to address the problem of keyboard occlusion.

2. code example
ZYViewController. h

 

[Plain]
# Import <UIKit/UIKit. h>
 
@ Interface ZYViewController: UIViewController <UITextFieldDelegate>
 
@ Property (nonatomic, strong) UITextField * myTextField;
 
@ End

# Import <UIKit/UIKit. h>

@ Interface ZYViewController: UIViewController <UITextFieldDelegate>

@ Property (nonatomic, strong) UITextField * myTextField;

@ End
ZYViewController. m

 

[Plain]
@ Synthesize myTextField;
 
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Self. view. backgroundColor = [UIColor underPageBackgroundColor];
MyTextField = [[UITextField alloc] init]; // initialize UITextField
MyTextField. frame = CGRectMake (35,230,250, 35 );
MyTextField. delegate = self; // sets the proxy.
MyTextField. borderStyle = UITextBorderStyleRoundedRect;
MyTextField. contentVerticalAlignment = uicontrolcontentverticalignmentcenter; // center vertically
MyTextField. placeholder = @ "Please entry your content! "; // Default text when the content is empty
MyTextField. returnKeyType = UIReturnKeyDone; // set the style of the put back button.
MyTextField. keyboardType = UIKeyboardTypeNumbersAndPunctuation; // you can specify the keyboard style as a number.
[Self. view addSubview: myTextField];


// Register the notification when the keyboard appears or is hidden
[[Nsicationcenter center defacenter center] addObserver: self
Selector: @ selector (keyboadWillShow :)
Name: UIKeyboardWillShowNotification
Object: nil];
[[Nsicationcenter center defacenter center] addObserver: self
Selector: @ selector (keyboardWillHide :)
Name: UIKeyboardWillHideNotification
Object: nil];
// Add a gesture and click to close the keyboard in other areas of the screen
UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (hideKeyboard)];
Gesture. numberOfTapsRequired = 1; // Number of gesture strikes
[Self. view addGestureRecognizer: gesture];
}
 
// Events called when the keyboard appears
-(Void) keyboadWillShow :( NSNotification *) note {
NSDictionary * info = [note userInfo];
CGSize keyboardSize = [[info objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue]. size; // frame of the keyboard
CGFloat offY = (460-keyboardSize.height)-myTextField. frame. size. height; // screen height-keyboard height-UITextField height
[UIView beginAnimations: nil context: NULL]; // Add an animation here to make the change smoother.
[UIView setAnimationDuration: 0.3]; // sets the animation time in seconds.
MyTextField. frame = CGRectMake (35, offY, 250, 35); // move the y coordinate of the UITextField to offY
[UIView commitAnimations]; // starts the animation effect.

}
// Events called when the keyboard disappears
-(Void) keyboardWillHide :( NSNotification *) note {
[UIView beginAnimations: nil context: NULL]; // Add an animation here to make the change smoother.
[UIView setAnimationDuration: 0.3];
MyTextField. frame = CGRectMake (35,230,250, 35); // restore the UITextField position
 
[UIView commitAnimations];
}
// Hide the keyboard
-(Void) hideKeyboard {
[MyTextField resignFirstResponder];
}
# Pragma mark-
# Pragma mark UITextFieldDelegate
// Start editing:
-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField
{
Return YES;
}
 
// Click the return button to perform the following actions:
-(BOOL) textFieldShouldReturn :( UITextField *) textField
{
[TextField resignFirstResponder]; // cancel the first response
Return YES;
}
 
// Edited:
-(Void) textFieldDidEndEditing :( UITextField *) textField
{

}
 
-(Void) viewDidDisappear :( BOOL) animated {
[Super viewDidDisappear: animated];
[[Nsicationcenter center defacenter center] removeObserver: self]; // remove the observer
}

@ Synthesize myTextField;

-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Self. view. backgroundColor = [UIColor underPageBackgroundColor];
MyTextField = [[UITextField alloc] init]; // initialize UITextField
MyTextField. frame = CGRectMake (35,230,250, 35 );
MyTextField. delegate = self; // sets the proxy.
MyTextField. borderStyle = UITextBorderStyleRoundedRect;
MyTextField. contentVerticalAlignment = uicontrolcontentverticalignmentcenter; // center vertically
MyTextField. placeholder = @ "Please entry your content! "; // Default text when the content is empty
MyTextField. returnKeyType = UIReturnKeyDone; // set the style of the put back button.
MyTextField. keyboardType = UIKeyboardTypeNumbersAndPunctuation; // you can specify the keyboard style as a number.
[Self. view addSubview: myTextField];


// Register the notification when the keyboard appears or is hidden
[[Nsicationcenter center defacenter center] addObserver: self
Selector: @ selector (keyboadWillShow :)
Name: UIKeyboardWillShowNotification
Object: nil];
[[Nsicationcenter center defacenter center] addObserver: self
Selector: @ selector (keyboardWillHide :)
Name: UIKeyboardWillHideNotification
Object: nil];
// Add a gesture and click to close the keyboard in other areas of the screen
UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (hideKeyboard)];
Gesture. numberOfTapsRequired = 1; // Number of gesture strikes
[Self. view addGestureRecognizer: gesture];
}

// Events called when the keyboard appears
-(Void) keyboadWillShow :( NSNotification *) note {
NSDictionary * info = [note userInfo];
CGSize keyboardSize = [[info objectForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue]. size; // frame of the keyboard
CGFloat offY = (460-keyboardSize.height)-myTextField. frame. size. height; // screen height-keyboard height-UITextField height
[UIView beginAnimations: nil context: NULL]; // Add an animation here to make the change smoother.
[UIView setAnimationDuration: 0.3]; // sets the animation time in seconds.
MyTextField. frame = CGRectMake (35, offY, 250, 35); // move the y coordinate of the UITextField to offY
[UIView commitAnimations]; // starts the animation effect.

}
// Events called when the keyboard disappears
-(Void) keyboardWillHide :( NSNotification *) note {
[UIView beginAnimations: nil context: NULL]; // Add an animation here to make the change smoother.
[UIView setAnimationDuration: 0.3];
MyTextField. frame = CGRectMake (35,230,250, 35); // restore the UITextField position

[UIView commitAnimations];
}
// Hide the keyboard
-(Void) hideKeyboard {
[MyTextField resignFirstResponder];
}
# Pragma mark-
# Pragma mark UITextFieldDelegate
// Start editing:
-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField
{
Return YES;
}

// Click the return button to perform the following actions:
-(BOOL) textFieldShouldReturn :( UITextField *) textField
{
[TextField resignFirstResponder]; // cancel the first response
Return YES;
}

// Edited:
-(Void) textFieldDidEndEditing :( UITextField *) textField
{

}

-(Void) viewDidDisappear :( BOOL) animated {
[Super viewDidDisappear: animated];
[[Nsicationcenter center defacenter center] removeObserver: self]; // remove the observer
}
Running result:

Initial status

 



Click the input box

 
 


 

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.