监听状态栏的区域的点击

当用户点击状态栏时,若只有一个UIScrollView的scrollsToTop属性为YES,该滚动视图将滑动到顶部。若存在多个,需手动设置scrollsToTop属性,或者通过监听状态栏点击事件在AppDelegate中处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值