[英]UIButton tap not responding
我添加了一個新的UIButton
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newButton setFrame:CGRectMake(x, y, width, height)];
[newButton setTitle:@"" forState:UIControlStateNormal];
[newButton setTag:x+INDEX_OFFSET];
[newButton setBackgroundImage:image forState:UIControlStateNormal];
[newButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:newButton];
[self bringSubviewToFront:newButton];
我可以在屏幕上看到它,但它沒有響應輕擊手勢。 它應該調用“buttonPressed”。 任何幫助都會超級!
可能的問題之一是您在超視圖的框架位置添加按鈕。 只是為了檢查它,嘗試在其超視圖邊界中將clipToBounds
屬性設置為YES,然后運行應用程序。 如果您沒有看到按鈕,則意味着您將按鈕位置設置為超出位置,這就是它不響應觸摸的原因。
另一個可能的問題是,如果您將UIButton
作為subview
添加到UIImageView
(您不應該,但它可以工作) 。 在這種情況下,您可以將其添加到UIView
或將UIImageView
的userInteractionEnabled
屬性設置為YES
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.