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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.