iOS の「見えるけど、押せないボタン」

Naohiro Oogatta
Jul 28, 2017 · 3 min read

昔から iOS アプリを作ってる方にはあたりまえのことらしいんですが、自分今日初めて知ってびっくりしたので書き残します。

そもそも iOS のボタンが「押せる( tappable )」かどうかの条件は

UIView のツリーを、対象の UIButton からルート要素まで親へ親へ辿っていくまでの途中の要素全てが User Interaction Enabled であること

というのはわかっていました。

UIView の中に UIButton があります

たとえばこの、 User Interaction Enabled な UIView に包まれた真っ赤な UIButton は押せます。当然です。

こんな感じです

何も問題ないです。

でも昨日 iOS 11 の UIBarButtonItem と格闘していたら、そこに見えている UIButton が押せなくなってしまいました。

原因は、 Constraint がおかしくなってその UIButton の superView である UIView の height が 0 になってしまったためだったのでした。

真っ赤な UIButton を包む UIView の height が 0 の状態です
height が 0 です

「height が 0 なのに Clip to Bounds が無効」な UIView に包まれている場合、その UIButton はまったくタップに反応しません。知らなかった!めっちゃ見えているのに!

UITapGestureRecognizer でやろうがなにしようが駄目、というのも徹底しているなと思いました。

今日も勉強になったなあ。

    Written by

    making services in small teams.

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade