UIScrollViewが複数ある時でも楽にステータスバーへのタッチイベントを取得する
scrollViewShouldScrollToTopを使うのが常套らしいのだけれどUIScrollViewが複数ある場合、1つを除いてすべてのUIScrollViewやUITextView/UITableViewなどのscrollsToTopをfalseにするのも楽ではないので touchesBegan を使いました。
AppDelegate.swift
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { super.touchesBegan(touches, withEvent: event) let touch = touches.first as! UITouch let location = touch.locationInView(self.window) if CGRectContainsPoint(UIApplication.sharedApplication().statusBarFrame, location) { NSNotificationCenter.defaultCenter().postNotificationName("statusBarTouched", object: nil) } }
ViewController.swift
override func viewWillAppear(animated: Bool) { NSNotificationCenter.defaultCenter().addObserver(self, selector: "statusBarTouched:", name: "statusBarTouched", object: nil) } override func viewDidDisappear(animated: Bool) { super.viewDidDisappear(animated) NSNotificationCenter.defaultCenter().removeObserver(self) } func statusBarTouched(notification: NSNotification) { }