IOS Development --- keyboard overlay control textfield

Source: Internet
Author: User

During the development process, we often encounter a problem: when we select the input box, the keyboard jumps out and overwrites our input box. The problem we need to solve is: move the input box up so that it is above the keyboard:

Prerequisites:

Declare a textfield in your h file

@property (retain, nonatomic) IBOutlet UITextField *textView;

First, the viewdidload method is as follows:

- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardwasShown:) name:UIKeyboardDidShowNotification object:nil];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardwasHidden:) name:UIKeyboardDidHideNotification object:nil];}
 
 

In this method, we call two methods: processing the listening event when the keyboard is displayed, and processing the listening event when the keyboard disappears.

(1) Handling of listening events during keyboard display

-(void)keyboardwasShown:(NSNotification *) notify{    CGRect frame = [self.view frame];    if ([[notify name]isEqualToString:UIKeyboardDidShowNotification]) {        [UIView animateWithDuration:0.3 animations:^{            self.view.frame = frame;        }completion:^(BOOL finished) {            //        }];    }        NSDictionary *info = [notify userInfo];    NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];        CGSize keyBoardSize = [aValue CGRectValue].size;    CGRect rect = self.textView.frame;    rect.origin.y = self.view.frame.size.height - keyBoardSize.height - rect.size.height;    self.textView.frame = rect;    }

(2) handling of listening events when the keyboard disappears

-(void) keyboardwasHidden:(NSNotification *) notify{    NSDictionary *info = [notify userInfo];    NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];        CGSize keyBoardSize = [aValue CGRectValue].size;            CGRect rect = self.textView.frame;    rect.origin.y = self.view.frame.size.height - rect.size.height;    self.textView.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.