0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UIButtonはUIScrollViewの下に配置するとき、isHighlightedを設定しても一瞬のタップでbackgroundColor変わらない問題

Posted at

普通にこのカスタマイズを使ったらタップでもボタンの背景色が変わります

class CustomButton: UIButton {
    open override var isHighlighted: Bool {
        didSet {
            backgroundColor = isHighlighted ? .buttonActive : .buttonDefault
        }
    }
}

こういうView配置になると、長押す時だけ反応します。

- UIScrollVIew
    - UIButton

原因はUIScrollViewのDelay Touch Downを外さないといけないです。
スクリーンショット 2021-12-08 13.23.52.png

あと、UIPageViewのsubviewsにUIScrollViewも存在するので、ご注意を。
対処方法はこちら!

public override func viewDidAppear(_ animated: Bool) {
    for view in self.view.subviews {
        if view is UIScrollView {
            (view as? UIScrollView)!.delaysContentTouches = false
        }
    }
}

参考

https://qiita.com/tetsuhama/items/e09446206dc13559802d
https://stackoverflow.com/questions/41371479/uibutton-only-looks-clicked-highlighted-on-longpress
https://stackoverflow.com/questions/49593158/how-to-fix-highlighted-state-uibutton-delay-in-uipageviewcontroller-page

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?