將 UITextField 作為子視圖添加到 UIImageView 上,運行後 TextField 點擊無反應、彈不出鍵盤、無法編輯。
UIImageView 繼承自 UIView,默認 userInteractionEnabled = NO。 父視圖不接收觸摸事件,子視圖自然也收不到響應鏈傳遞的觸摸。
UIImageView *person = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"geren"]];
person.userInteractionEnabled = YES; // 關鍵:開啟交互
// ... 後續添加 TextField
UIView 或 UIControl 作為父視圖,再分別添加 UIImageView (背景) 和 UITextField (輸入)。userInteractionEnabled = YES**。UILabel、UIImageView 默認關閉交互;UIButton、UIControl、UITextField 默認開啟。userInteractionEnabled = NO,繼續向下穿透找下一個。touchesBegan 等事件。父視圖關閉交互 = 斷開子視圖接收觸摸的路徑。
UIImageView 添加 UITextField 無法編輯,**核心修復:imageView.userInteractionEnabled = YES**。更推薦用 UIView 做容器,語義清晰且避坑。