IOS ScrollView Scrolling controls use

Source: Internet
Author: User

1.ScrollView Overview:

ScrollView is the Uiscrollview class, which is a view of the container type.

It has two subclasses: Uitextview and UITableView, which provide horizontal or vertical scroll bars when the content goes out of the screen.


2. It consists of 3 properties:

Contentsize: Represents a content view in ScrollView

Contentinset: Used to add a border around the content view in ScrollView, often in order to leave space to place toolbars, labels, or navigation bars, etc.

Contentoffset: The offset of the content view coordinate origin from the origin of the ScrollView coordinate.


Here we take an example of the initial study of ScrollView:


Main completion: Basic view construction, click the text box appears after the keyboard, let the text box automatically scroll to the keyboard, click on the Keyboard return button and then close the keyboard.


• The above is the implementation diagram, the following process analysis.


(1) Constructs a basic view: Includes three buttons and a text box (one of the buttons is out of view screen range and needs to be dropped to see)

Note that all controls in the ScrollView are adjusted to absolute positioning, and the distance from the top border is the absolute distance. This way their Y coordinate will not change after the height of the parent view (ScrollView) has been adjusted.

(2) Add output for ScrollView and TextField controls

@property (Weak, nonatomic) Iboutlet Uiscrollview *scrollview; @property (weak, nonatomic) Iboutlet Uitextfield * TextField;

(3) Add the following code to the VIEWCONTROLLER.M:

@implementation viewcontroller{    BOOL keyboardvisible;}
Declaring variables: Keyboard open identity


-(void) viewdidload {    [super viewdidload];    Self.scrollView.contentSize = Cgsizemake (320,700);    Self.textField.delegate = self;  Implements the Uitextfielddelegate protocol, where the delegate is set to itself. }
This is a function that is automatically triggered when the view loads: Set the size of the ScrollView, and set the delegate


Asks the delegate if the text field should process the pressing of the return button.//description when the return button button is pressed, hide the keyboard-(BOO L) Textfieldshouldreturn: (Uitextfield *) textfield{    [TextField Resignfirstresponder];      return YES;}
A function that fires when you press the Return button button on the keyboard to implement the Close button action

-(void) Keyboarddidshow: (nsnotification *) notif{if (keyboardvisible) {return;    }//Get keyboard size Nsdictionary * info = [Notif userInfo];    Nsvalue *avalue = [info objectforkey:uikeyboardframeenduserinfokey];    Cgsize keyboardsize = [Avalue cgrectvalue].size;    Redefine the size of the ScrollView cgrect viewframe = self.scrollView.frame;    ViewFrame.size.height-= (keyboardsize.height);    Self.scrollView.frame = Viewframe;    Scroll to the current text box cgrect 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; }
First function: When the keyboard appears, get the keyboard size, redefine the size of the ScrollView, and scroll to the current text box

Second function: Restore dimensions


Of course, there is no function for the above two defined functions, because no one calls it and it is not triggered (it is a function of its own definition).

These two functions are called when the view Willappear and viewwilldisappear, to achieve the appropriate functionality:

View Method-(void) Viewwillappear: (BOOL) Animated {    //register keyboard appears notification    [[Nsnotificationcenter Defaultcenter] Addobserver : Self selector: @selector (keyboarddidshow:)                                                 name:uikeyboarddidshownotification Object:nil];    Register for keyboard hide Notifications    [[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (keyboarddidhide:)                                                 Name:uikeyboarddidhidenotification Object:nil];    [Super viewwillappear:animated];} -(void) Viewwilldisappear: (BOOL) Animated {    //unlock keyboard notifications    [[Nsnotificationcenter Defaultcenter] Removeobserver : Self                                                    name:uikeyboarddidshownotification object:nil];    Unlock keyboard Hide Notifications    [[Nsnotificationcenter defaultcenter] removeobserver:self                                                    Name: Uikeyboarddidhidenotification Object:nil];    [Super viewwilldisappear:animated];}

Also note that there is also a need to register the keyboard to show hidden notifications (and contact)

After registration, when the corresponding behavior occurs (in this case, the keyboard appears and hidden), it will call your implementation of the function


At this point, the use of the secondary ScrollView is complete.



For iOS programs, where the protocol agent, auto-trigger function, custom function, at the beginning of the understanding of the confusion, do not know what function at what time is called, how to set, etc., after learning this program and I want to have the same problem beginners can understand a little bit ...








IOS ScrollView Scrolling controls use

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.