The keyboard blocks the input box.

Source: Internet
Author: User

The keyboard blocks the input box.

IOS development-keyboard notificationPreviously, when the keyboard of the editing event in the input box is triggered, the current View is always moved up, and the editing end is moved down, which is time-consuming and inefficient.

I read the keyboard notification method on the Internet, which is very convenient. So I wrote a demo for beginners to refer!

 

1. declare in the ViewController. m file

# Import "ViewController. h "@ interface ViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate> @ property (nonatomic, strong) UITableView * tableView; // custom table TableView @ end @ implementation ViewController


2. initialize and add a notification observer

1-(void) viewDidLoad {2 [super viewDidLoad]; 4 self. tableView = [[UITableView alloc] initWithFrame: [UIScreen mainScreen]. bounds style: UITableViewStylePlain]; 5 self. tableView. delegate = self; 6 self. tableView. dataSource = self; 7 [self. view addSubview: self. tableView]; 8 9 // notification 10 when the keyboard is to be displayed [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (boardWillShow :) name: UIKeyboardWillShowNotification object: nil]; 11 // notification 12 when the keyboard is about to end [[NSNotificationCenter defacenter center] addObserver: self selector: @ selector (boardDidHide :) name: UIKeyboardDidHideNotification object: nil]; 13}

3. Implementation of notification response methods

1-(void) boardWillShow :( NSNotification *) sender {2 // obtain the keyboard size 3 CGRect keyBoardRect = [sender. userInfo [UIKeyboardFrameEndUserInfoKey] CGRectValue]; 4 // when the keyboard is to be displayed, increase the bottom margin of tableView and change it to the keyboard height of 5 self. tableView. contentInset = UIEdgeInsetsMake (0, 0, keyBoardRect. size. height, 0); 6} 7 8-(void) boardDidHide :( NSNotification *) sender {9 // when the keyboard is about to disappear, the margin is restored to the initial state 10 self. tableView. contentInset = UIEdgeInsetsZero; 11}

4. UITextField proxy event (click the return button on the keyboard to hide the keyboard)

1-(BOOL) textFieldShouldReturn :( UITextField *) textField {2 // cancel the first responder of the current input box 3 [textField resignFirstResponder]; 4 return YES; 5}

5. Proxy method of tableView

-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return 15;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * ider = @ "cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: ider]; if (! Cell) {cell = [[UITableViewCell alloc] initWithStyle: Invalid reuseIdentifier: ider];} UITextField * TF = [[UITextField alloc] initWithFrame: CGRectMake (100, 5,150, 20)]; TF. placeholder = @ "enter"; TF. delegate = self; // Add proxy [cell. contentView addSubview: TF]; cell. textLabel. text = @ "test"; return cell ;}
@ End

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