[英]addSubView to UIButton
我正在嘗試將子視圖添加到 UIButton。 這現在工作正常。 但是,一旦我添加了子視圖,該按鈕就不再可點擊了。
我使用以下代碼:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
[button addSubview:asyncImage];
[button addSubview:price];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
如果我剪掉 2 個 addsubviews,該按鈕將再次起作用。 如果有人知道如何解決這個問題,那就太好了!
太好了!!!
我找到了一個快速的解決方案。 我需要將 asyncimageview 設置為以下內容:
asyncImage.userInteractionEnabled = NO;
asyncImage.exclusiveTouch = NO;
在這之后它起作用了!
嘗試:
[UIButton buttonWithType:UIButtonTypeCustom];
代替:
[UIButton buttonWithType:UIButtonControlType];
這里重要的是確保將userInteractionEnabled
設置為NO
。 幸運的是,它立即適用於UIImageView
和UILabel
(可能適用於UIView
的其他子類,但這些是添加到按鈕的最流行的子視圖),因為默認情況下,此類的默認設置為NO
。 不幸的是,它在UIView
中設置為YES
,因此請確保在這種情況下更改它。 沒有必要與任何其他標志混在一起。 問題的本質是很多人不知道這個標志的默認值在子類中是不同的。
你有沒有試過把:
[asyncImage setUserInteractionEnabled:YES];
在同一情況下,我執行此操作:從 UIButton 繼承並將按鈕的所有標簽和圖像視圖添加到 self,最后將新按鈕作為最后一個子視圖查看並將 self 按鈕的目標添加到最后一個按鈕(也將 backgroundColor 設置為 clearColor 以實現透明)。 現在它將是可點擊的並且工作正常。
在 Swift 中,測試您的 UIButton 是否被阻止
uibtn.userInteractionEnabled = false
uibtn.exclusiveTouch = false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.