iOS Development-project requirements-quickly back to the top of the current interface

Source: Internet
Author: User

Fast reach top with UIWindow

For example, add a uiwindow with no color in the status bar (add a button inside), which allows you to quickly return to the top of the current interface when clicking this button.

Core code

First, the use of UIWindow to reach the top
1. Create a new window
1) UIWindow level, the higher the level, the more it appears on the upper level (high window cover on the lower level window)
Uiwindowlevelnormal < Uiwindowlevelstatusbar < Uiwindowlevelalert

@interfaceappdelegate () @property (nonatomic, strong) UIWindow*Topwindow;@end@implementationappdelegate-(UIWindow *) topwindow{if(_topwindow = =Nil) {_topwindow=[[UIWindow alloc] init]; _topwindow.windowlevel=Uiwindowlevelalert; _topwindow.frame= CGRectMake (0,0, Chgscreenw, -); _topwindow.backgroundcolor=[Uicolor Clearcolor]; //This is important, the default is hidden, want to display must be manually set to No_topwindow.hidden =NO; [_topwindow Addgesturerecognizer:[[uitapgesturerecognizer alloc] initwithtarget:self action: @selector (    Topwindowclick)]; }    return_topwindow;}

2. Traverse each window

-(void) topwindowclick{    *windows = [uiapplication sharedapplication].windows;      for inch windows) {
Traverse all child controls under the window [self Searchsubviews:window];} }

3. Traverse all child controls under the window

/** * Search all child controls inside the Superview*/- (void) Searchsubviews: (UIView *) superview{ for(Uiscrollview *scrollviewinchsuperview.subviews) {[Self searchsubviews:scrollview]; //determine if it is ScrollView        if(! [ScrollView Iskindofclass:[uiscrollviewclass]])Continue; //determine if the ScrollView is on the window (intersect)//calculates the ScrollView rectangle on the window coordinate systemCGRect Scrollviewrect =[ScrollView convertRect:scrollView.bounds ToView:scrollView.window]; CGRect Windowrect=ScrollView.window.bounds; //determines whether the ScrollView border crosses the window border        if(! Cgrectintersectsrect (Scrollviewrect, Windowrect))Continue; //can come here to explain scrollview on the window, modify the offset, roll to the topCgpoint offset =Scrollview.contentoffset; Offset.y=  -ScrollView.contentInset.top;    [ScrollView Setcontentoffset:offset Animated:yes]; }}

4. Active call lazy Loading when program is active

// when the program is activated, call Topwindow-(void) Applicationdidbecomeactive: (uiapplication *) application {    [ Self Topwindow];}

iOS Development-project requirements-quickly back to the top of the current interface

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.