Workarounds for iOS Keyboard occlusion issues

Source: Internet
Author: User

One of the workarounds for keyboard occlusion input boxes in iOS development--Keyboard notifications before dealing with this problem, always in the trigger input box Edit Event keyboard pop-up, the current view as a whole upward movement, the end of editing and the overall downward shift, time-consuming and inefficient.

On the Internet to see the use of Keyboard notification method is very convenient, so wrote a demo for beginners reference!

1. In the VIEWCONTROLLER.M file statement

"ViewController.h"@interface Viewcontroller () <uitableviewdelegate,uitableviewdatasource, Uitextfielddelegate>@property (nonatomic,strong) UITableView *tableview;  Custom table TableView@end@implementation viewcontroller       


2. Initialize and add notification Viewer

1-(void) Viewdidload {2[Super Viewdidload];4 Self.tableview =[[UITableView alloc] Initwithframe:[uiscreen mainscreen].bounds Style:uitableviewstyleplain];5 Self.tableview.Delegate =Self6 Self.tableView.dataSource = self;  [Self.view addsubview:self.tableview]; 8  9 // The keyboard will be displayed Span style= "color: #008080;" >10 [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (boardwillshow:) Name: Uikeyboardwillshownotification object:nil];11 // The keyboard is about to end when the notification 12 [[ Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (boarddidhide:) Name: Uikeyboarddidhidenotification object:nil];13}                 

3. How to implement notification response

1-(void) Boardwillshow: (Nsnotification *) sender{2//Get the size of the keyboard3 CGRect keyboardrect=[Sender.userinfo[uikeyboardframeenduserinfokey] cgrectvalue];// When the keyboard is going to be displayed, Change the bottom margin of the tableView to the height of the keyboard  5 Self.tableView.contentInset = Uiedgeinsetsmake (0, 0, KeyBoardRect.size.height, 0); } 7  8-(void) Boarddidhide: (nsnotification *) sender{< Span style= "color: #008080;" > 9 //10 self.tableView.contentInset = uiedgeinsetszero;< Span style= "color: #008080;" >11}                 

4.UITextField Agent Event (click the Return button in the keyboard to hide the keyboard)

1-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{2     // cancels the first responder of the current input box [TextField Resignfirstresponder]; return YES;  5}       

Proxy method for 5.tableView

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{Return15;} -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static NSString *ider =@"Cell"; UITableViewCell *cell =[TableView Dequeuereusablecellwithidentifier:ider];if (!Cell) {cell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:ider]; } Uitextfield *TF = [[Uitextfield alloc] Initwithframe:cgrectmake (5, (+   )]; Tf.placeholder = @ " Please enter "; Tf. delegate =self; // text box add Agent [Cell.contentview ADDSUBVIEW:TF]; cell.textLabel.text = @ " test "; return Cell;}
@end

6. End!

Workarounds for iOS Keyboard occlusion issues

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.