簡體   English   中英

UIButton點擊無響應

[英]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或將UIImageViewuserInteractionEnabled屬性設置為YES

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM