簡體   English   中英

添加UILabel作為UIButton的子視圖不會觸發按鈕操作

[英]adding a UILabel as subview of UIButton doesn't trigger button action

我有一個UIButton ,我添加一個子視圖到UIButton這是一個UILabel 當我點擊UILabel時,按鈕的動作不會被觸發。 是否有一個簡單的解決方案,以便在輕觸UILabel時將觸摸事件傳遞給UIButton
我正在考慮向UILabel添加手勢識別器,然后觸發UIButton的動作,不確定是否有更簡單的方法。

您可以禁用用戶與標簽的互動。

_yourLabel.userInteractionEnabled = NO;

嘗試這個,

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *firstButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [firstButton setTitle:@"first" forState:UIControlStateNormal];
    firstButton.frame=CGRectMake(40, 70, 80, 80);
    [firstButton addTarget:self action:@selector(firstButtonClicked) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview:firstButton];

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    label.text=@"hai";
    label.backgroundColor=[UIColor redColor];
    [firstButton addSubview:label];

}

-(void)firstButtonClicked {
    NSLog(@"first");
}

暫無
暫無

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

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