點擊按鈕沒有任何觸發事件,也沒有高亮狀態變化,彷彿按鈕不存在。
contentView 上疊加了透明的 UIImageView、UIView、或 label 的 frame 超出預期,攔截了觸控事件。bringSubviewToFront)、縮小遮擋 view 的 frame、或設定遮擋 view 的 userInteractionEnabled = NO。UIButton → cell.contentView → cell → tableView → ...cell.contentView.userInteractionEnabled = NO 或 cell.userInteractionEnabled = NO,觸控事件傳遞鏈中斷,按鈕永遠收不到事件。| 陷阱 | 症狀 | 解決 |
|---|---|---|
| Cell 高度過小,按鈕 frame 超出 contentView bounds | 按鈕可見但點不到邊緣區域 | 啟用 clipsToBounds = YES 於 contentView,或調整 row height / 按鈕 frame |
使用 UITableViewCellStyle 預設樣式卻手動加按鈕 | 系統自動佈局可能覆蓋你的按鈕 | 改用 UITableViewCellStyleDefault 並只在 contentView 加 subview,或完整自訂佈局 |
| 手勢識別器攔截 | 加在 tableView 或 cell 上的 UITapGestureRecognizer 搶走觸控 | 設定 gestureRecognizer.cancelsTouchesInView = NO 或調整 delegate |
| iOS 7 模擬器已知 Bug | 真機正常,模擬器失效 | 升級 Xcode / 模擬器版本,或改用真機測試 |
userInteractionEnabled = YESTouch Up Inside,斷點能命中cancelsTouchesInView = NOXIB 裡的按鈕點不了,**90% 是視圖層級遮擋或 userInteractionEnabled 鏈路中斷**。善用 Debug View Hierarchy 一眼看穿結構,配合 Action 斷點 驗證事件傳遞,幾分鐘就能定位問題。別再盲目猜測,工具才是最高效的除錯夥伴。