IOS prevents keyboard occlusion

Source: Internet
Author: User

The development of a friend may often encounter the click on the input box to activate the keyboard caused by the input box is obscured, there are two common solutions:

1. When the keyboard appears, move the content page up appropriately

2, when the keyboard appears, floating input box to the appropriate location the author thinks the first kind of scheme is relatively simple, here only the first one, the second similar.

First, the keyboard event monitoring settings (can be written in viewdidload):

Increased monitoring, when the keyboard appears or changes when the message is received [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillshow:) Name:uikeyboardwillshownotification object:nil];//increased monitoring when the key exits when the message is received [[Nsnotificationcenter Defaultcenter] AddObserver : Self selector: @selector (keyboardwillhide:) name:uikeyboardwillhidenotification Object:nil];

Second, in the listening event two methods to handle the view of the move up and down, the distance can be based on the height of the keyboard//implementation when the keyboard appears when the height of the keyboard to calculate the size. For the input box display location

-(void) Keyboardwillshow: (nsnotification*) anotification {nsdictionary* info = [Anotification userinfo];// Kbsize is the size of the keyboard (width, height) cgsize kbsize = [[Info objectforkey:uikeyboardframeenduserinfokey] cgrectvalue].size;// Get the height of the keyboard keyboardhight=kbsize.height;//will uiscrollview on the move hashkeyboard=yes;//set the name of the animation [UIView beginanimations:@ " Animationopen "context:nil];//Set animation interval time [UIView setanimationduration:0.20];//?? Starts the next animation using the currently running state [UIView setanimationbeginsfromcurrentstate:yes];//sets the displacement of the view movement Self.view.frame = CGRectMake ( Self.view.frame.origin.x, Self.view.frame.origin.y-keyboardhight, Self.view.frame.size.width, Self.view.frame.size.height);//Set animation end [UIView commitanimations]; }//down will not repeat

  

Original Blog Address: http://www.cnblogs.com/iosliu/p/4425024.html

IOS prevents keyboard occlusion

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.