iOS Development-click Status bar ScrollView back to top failure resolution

Source: Internet
Author: User

If there is only one scrollview in the current Viewcontroller, click on the status bar and the ScrollView will scroll to the top. But when there are multiple scrollview in the Viewcontroller, it doesn't work! At this time, how to be compatible?

Uiscrollview has such an attribute scrollstotop. hold command, click Scrollstotop to go in and you'll see an annotation about this property.

On the IPhone, we execute this gesture only if there's one on-screen scroll view with ' scrollstotop ' = = YES. If more than one is found, none would be scrolled.

Now know why not the spirit of it!!!

Recently done project, due to a horizontal screen scrollview inside placed multiple TableView, but click on the status bar still requires TableView scroll to the top, so to overcome this problem!

Although "Wei Zesi incident" Let us all despise Baidu, but in the case of Google still not come in, I silently Baidu a bit!

。。。。。。。。。。。。。。。。。。。。。。。。

After the problem was solved, I summed it up a bit.

The idea is this:

1 trace the Uistatusbar touch event and then respond to the event. Do not add events directly to StatusBar because there are no eggs to use! I don't know why there are no eggs!

2 Find the Keywindow of uiapplication and traverse all child controls of Keywindow. If it is ScrollView and is displayed in the current Keywindow, set its contentoffset y value to the original value (0). Recursion is used here to traverse.

First we track Uistatusbar touch events and need to add the following code to the Appdelegate

-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *)event  {    [super Touchesbegan: Touches withevent:event];     = [[[Event  alltouches] anyobject] locationInView:self.window];     = [UIApplication sharedapplication].statusbarframe;     if (Cgrectcontainspoint (statusbarframe, location)) {        [self statusbartouchedaction];}    }

Then in the Statusbartouchedaction method, the ScrollView that appears in the current Keywindow scroll to the top

-(void) statusbartouchedaction {    [Jmsuiscrollviewtool scrollviewscrolltotop];}

Here's a look at Jmsuiscrollviewtool

#import <Foundation/Foundation.h>@interface jmsuiscrollviewtool:nsobject+ (void) scrollviewscrolltotop; @end
#import"JMSUIScrollViewTool.h"@implementation Jmsuiscrollviewtool+ (void) scrollviewscrolltotop {UIWindow*window =[UIApplication Sharedapplication].keywindow; [Self Searchscrollviewinview:window];}+ (void) Statusbarwindowclick {UIWindow*window =[UIApplication Sharedapplication].keywindow; [Self Searchscrollviewinview:window];}+ (BOOL) Isshowingonkeywindow: (UIView *) View {UIWindow*keywindow =[UIApplication Sharedapplication].keywindow; CGRect Newframe=[Keywindow convertRect:view.frame FromView:view.superview]; CGRect Winbounds=Keywindow.bounds; BOOL intersects=Cgrectintersectsrect (Newframe, winbounds); return!view.ishidden && View.alpha >0.01&& View.window = = Keywindow &&intersects;}+ (void) Searchscrollviewinview: (UIView *) Supview { for(Uiscrollview *subviewinchsupview.subviews) {if([SubView Iskindofclass:[uiscrollviewclass]] &&[self Isshowingonkeywindow:supview]) {Cgpoint offset=Subview.contentoffset; Offset.y= -SubView.contentInset.top;        [SubView Setcontentoffset:offset Animated:yes];    } [self Searchscrollviewinview:subview]; }} @end

Next, let's get your project running!

There should be applause here ...

Reference: Http://stackoverflow.com/questions/3753097/how-to-detect-touches-in-status-bar

Http://www.cocoachina.com/ios/20150807/12949.html

iOS Development-click Status bar ScrollView back to top failure resolution

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.