First, Case introduction: The use of uiscrollview, to avoid the keyboard occlusion control; Figure 01, Figure 02 is actually a third UIButton, sliding screen can be seen, 06. Effect 01, Figure 02.
Fig. 01 FIG. 02 Fig. 03 Fig. 06
Second, the case steps:
1, choose single View application New project, named Cq.30.scrollview,03.
2, MAIN.STORYBOARD04, figure 05.
Fig. 04 Fig. 05
3, CQ30ViewController.h
#import <UIKit/UIKit.h>@interface cq30viewcontroller:uiviewcontroller< Uitextfielddelegate>{ BOOL keyboardvisible; // Keyboard appearance Identification **TextField; @end
4, CQ30VIEWCONTROLLER.M
"By Uitextfielddelegate to Uitextfield discard the first responder identity, turn off the keyboard
#pragma Mark--uitextfielddelegate method-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{ [ TextField Resignfirstresponder]; return YES;}
"Register for monitoring keyboard open and close notifications
-(void) Viewwillappear: (BOOL) Animated {//Sign up for keyboard notifications[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (keyboarddidshow:) Name:uikeyboarddidshownotificationObject: nil]; //Register for keyboard Hide notifications[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (keyboarddidhide:) Name:uikeyboarddidhidenotificationObject: nil]; [Super viewwillappear:animated];}-(void) Viewwilldisappear: (BOOL) Animated {//unblock keyboard Notifications[[Nsnotificationcenter Defaultcenter] removeobserver:self name: UikeyboarddidshownotificationObject: nil]; //To unlock a keyboard hide notification[[Nsnotificationcenter Defaultcenter] removeobserver:self name: UikeyboarddidhidenotificationObject: nil]; [Super viewwilldisappear:animated];}
The Uiscrollview frame is dynamically changed by monitoring the opening and closing of the keyboard, focusing on the current Uitextfield.
-(void) Keyboarddidshow: (Nsnotification *) Notif {if(keyboardvisible) {//keyboard has appeared to ignore notifications return; } //Get keyboard sizensdictionary* info =[Notif UserInfo]; Nsvalue* Avalue =[Info Objectforkey:uikeyboardframeenduserinfokey]; Cgsize keyboardsize=[Avalue cgrectvalue].size; //redefining the size of a scrollviewCGRect Viewframe =Self.scrollView.frame; ViewFrame.size.height-=(Keyboardsize.height); Self.scrollView.frame=Viewframe; //Scroll to the current text boxCGRect Textfieldrect =[Self.textfield frame]; [Self.scrollview Scrollrecttovisible:textfieldrect Animated:yes]; Keyboardvisible=YES;}-(void) Keyboarddidhide: (Nsnotification *) Notif {nsdictionary* Info =[Notif UserInfo]; Nsvalue* Avalue =[Info Objectforkey:uikeyboardframeenduserinfokey]; Cgsize keyboardsize=[Avalue cgrectvalue].size; CGRect Viewframe=Self.scrollView.frame; ViewFrame.size.height+=Keyboardsize.height; Self.scrollView.frame=Viewframe; if(!keyboardvisible) { return; } keyboardvisible=NO; }