Solution to keyboard masking input in iOS

Source: Internet
Author: User
Tags bool

Generally speaking, there are so many kinds of keyboard occlusion, one is to cover the Uitextview, there is to cover Uitextfield, in general, the comparison recommended in Uiscrollview or UITableView add TextField control. But sometimes it may be inevitable.

In the Uitextview

The approach is given in the Apple's official document, where the registered observer listens for Uikeyboardwillshow and Willhide events.

-(void) Viewwillappear: (BOOL) animated  
{  
    [super viewwillappear:animated];  
          
    [[Nsnotificationcenter Defaultcenter] addobserver:self  
                                             selector: @selector (keyboardwillshow:)  
                                                 Name: Uikeyboardwillshownotification  
                                               Object:nil];  
    [[Nsnotificationcenter Defaultcenter] addobserver:self  
                                             selector: @selector (keyboardwillhide:)  
                                                 Name: Uikeyboardwillhidenotification  
                                               Object:nil];  
}

Ways to implement Customizations

-(void) Keyboardwillshow: (nsnotification *) anotification 

 
{  
    nsdictionary *userinfo = [Anotification UserInfo] ;  
          
    CGRect keyboardrect = [[UserInfo Objectforkey:uikeyboardframeenduserinfokey] 

cgrectvalue];  
    Nstimeinterval animationduration = [[UserInfo 

Objectforkey:uikeyboardanimationdurationuserinfokey] DoubleValue ];  
          
    CGRect newframe = self.view.frame;  
    NewFrame.size.height-= keyboardRect.size.height;  
          
    [UIView beginanimations:@ "Resizetextview" context:nil];  
    [UIView setanimationduration:animationduration];  
          
    Self.view.frame = Newframe;  
          
    [UIView commitanimations];  
}

Get the information that the keyboard displays, and then adjust the view frame according to the information, then the Willhide method is the same as above, except to change the height of the new height to + = KeyboardRect.size.height, and finally, remove the Observer:

- 

(void) Viewdiddisappear: (BOOL) animated  
{  
    [super viewdiddisappear:animated];  
          
    [[Nsnotificationcenter Defaultcenter] removeobserver:self  
                                                    name:uikeyboardwillhidenotification  
                                                  Object:nil ];  
    [[Nsnotificationcenter Defaultcenter] removeobserver:self  
                                                    name:uikeyboardwillshownotification  
                                                  Object:nil ];  
}

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.