Ios.uikit.05.uiscrollview

Source: Internet
Author: User

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; }

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.