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