When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its scrollsToTop
property is YES, its delegate does not return NO from -scrollViewShouldScrollToTop:
, and it is not already at the top.
// On iPhone, we execute this gesture only if there’s one on-screen scroll view with scrollsToTop
== YES. If more than one is found, none will be scrolled.
@property(nonatomic) BOOL scrollsToTop __TVOS_PROHIBITED; // default is YES.
上面这句摘自苹果UIScrollView API解释, 意思是说当屏幕上只有一个ScrollView 的scrollsToTop 属性为YES时, 只要此时用户点击状态栏,那么此时这个唯一的scrollview 就会滚动到屏幕的顶部, 如果有多个scrollView 且多个scrollView的scrollsToTop属性为YES就不会执行.
方案一:
也就是说, 如果现在有多个scrollView, 比如:
scrollView1/ scrollView2/ scrollView3/ scrollView4/ scrollView5
此时想要点击状态栏时那个scrollview 滚动到顶部, 那么就设置那个scrollview 的scrollsToTop 为YES, 其他的为NO.
方案二
监听状态栏的点击
状态栏(StatusBar)和window其实2个独立的东西 没有父子关系, 可以发现状态栏(StatusBar) 其实是盖在window上的. 因此当我们点击状态栏时, window 是不能响应事件的, 此时只有AppDelegate 能处理.
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
/** 监听状态栏的点击
*/
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:nil]; // 当传nil表示取在屏幕中的位置
CGRect statusBarFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20);
if (CGRectContainsPoint(statusBarFrame, point)) {
NSLog(@"用户点击了状态栏");
// 在这里处理用户点击状态栏的交互动作即可
}
}
@end