ios-Customize click the status bar to roll back to the top

Source: Internet
Author: User

Click the status bar roll back to the top this function is the system comes with, only need to set up self.scrollView.scrollsToTop = YES ,

But this property has a premise that the window must have only one scrollable to have the View effect, this time you need to customize the creation of a window

To complete this function.

Add window
    • In AppDelegate creating a new window must give this window to set a root controller, otherwise it will error, here can be dispatch_after added to the window by a delay can not set the root controller
    • The window is level, the windowLevel higher the level the higher the display at the top, if the level is the same, then the added creation is displayed at the top. Level is divided into three types, UIWindowLevelAlert > UIWindowLevelStatusBar > UIWindowLevelNormal then create a create and add a monitor
1 /**2 * Show Top window3  */4+ (void) Show5 {6Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (0.25* nsec_per_sec)), Dispatch_get_main_queue (), ^{7Topwindow_ =[[UIWindow alloc] init];8Topwindow_.windowlevel =Uiwindowlevelalert;9Topwindow_.frame =[uiapplication sharedapplication].statusbarframe;TenTopwindow_.backgroundcolor =[Uicolor Clearcolor]; OneTopwindow_.hidden =NO; A [Topwindow_ Addgesturerecognizer:[[uitapgesturerecognizer alloc] initwithtarget:self action: @selector (Topwindo Wclick)]; -     }); - } the  - /** - * Listen to the top of the window click -  */ ++ (void) Topwindowclick - { +UIWindow *keywindow =[uiapplication Sharedapplication].keywindow; A [self Searchallscrollviewsinview:keywindow]; at } -  - /** - * Find all Uiscrollview in the parameter view -  */ -+ (void) Searchallscrollviewsinview: (UIView *) View in { -     //recursive traversal of all child controls to      for(UIView *subviewinchview.subviews) { + [self searchallscrollviewsinview:subview]; -     } the  *  $     //Determine the child control type (if not uiscrollview, return directly)Panax Notoginseng     if(! [View Iskindofclass:[uiscrollviewclass]])return; -  the     //found Uiscrollview. +Uiscrollview *scrollview = (Uiscrollview *) view; A  the     //determine if Uiscrollview is overlapping with window (if Uiscrollview does not overlap with window, return directly) +     if(! [ScrollView Bs_intersectswithanotherview:nil])return; -  $     //let Uiscrollview scroll to the front $     //Jean CGRectMake (0, 0, 1, 1) This rectangle is completely displayed in the frame frame of the ScrollView -[ScrollView Scrollrecttovisible:cgrectmake (0,0,1,1) Animated:yes]; -}

Note that there is a Uiscrollview classification, which is added to this method.

-(BOOL) Bs_intersectswithanotherview: (UIView *) anotherview{    if (Anotherview = = nil) Anotherview = [uiapplication sharedapplication].keywindow;     // determine if self and anotherview overlap    CGRect selfrect = [self convertRect:self.bounds toview:nil];     = [Anotherview convertRect:anotherView.bounds toview:nil];     return Cgrectintersectsrect (Selfrect, anotherrect);}

The above method can be used to determine whether 2 of the same coordinate system View overlap, and if anotherView it is empty, return to the window

Add the above code to the listening method to complete the function  

ios-Customize click the status bar to roll back to the top

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.