2012-11-27 134 views
0

我有一個標籤在上面的按鈕。標籤阻止按鈕,所以我不能點擊它。標籤封鎖按鈕點擊

timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 27)]; 
[self.view addSubview:timerView]; 

UIButton *timerBackground = [UIButton buttonWithType:UIButtonTypeCustom]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"] forState:UIControlStateNormal]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"] forState:UIControlStateHighlighted]; 
[timerBackground addTarget:self action:@selector(openTimeChanger) forControlEvents:UIControlEventTouchUpInside]; 
timerBackground.frame = CGRectMake(-2 , 15, 102, 27); 
[timerView addSubview:timerBackground]; 

DateDay = [[UILabel alloc] initWithFrame:CGRectMake(6, 4, 20, 21)]; 
DateDay.backgroundColor = [UIColor clearColor]; 
DateDay.textColor = [UIColor whiteColor]; 
[timerBackground addSubview:DateDay]; 

我希望標籤是'透明'的點擊,所以我可以點擊按鈕。

回答

1

UILabel不會阻止觸摸,但您的視圖層次。我已經添加了一些顏色調試此:

enter image description here

  1. timerView - 綠色
  2. timerBackground - 紅
  3. DateDay - 黑

正如你看到的,你的按鈕是延長它的父項,所以綠色範圍外的所有內容都是非活動的,實際上,如果你點擊黑色矩形的頂部,按鈕和父項在標籤下重疊 - 你將得到輕擊事件。

要解決這個問題,只需簡單地對齊視圖,使它們彼此相對。

0

試試這個,

DateDay.userInteractionEnabled = NO; 

還要檢查是否有在此之上沒有其他看法。並正確對齊您的視圖以檢測觸摸。如果您將框架設置爲CGRectMake(-2 , 15, 102, 27);,則timerView之外的區域不會檢測到任何觸摸。

檢查,如果你可以如下修改代碼來檢測觸摸,

timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 27)]; 
[self.view addSubview:timerView]; 

UIButton *timerBackground = [UIButton buttonWithType:UIButtonTypeCustom]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"] forState:UIControlStateNormal]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"] forState:UIControlStateHighlighted]; 
[timerBackground addTarget:self action:@selector(openTimeChanger) forControlEvents:UIControlEventTouchUpInside]; 
timerBackground.frame = CGRectMake(0, 0, 100, 27); 
[timerView addSubview:timerBackground]; 

DateDay = [[UILabel alloc] initWithFrame:CGRectMake(6, 4, 20, 21)]; 
DateDay.backgroundColor = [UIColor clearColor]; 
DateDay.textColor = [UIColor whiteColor]; 
[timerBackground addSubview:DateDay]; 

如果你仍然想保持你的timerBackground的幀CGRectMake(-2 , 15, 102, 27);,將其添加爲self.view一個子視圖,而不是增加對timerView並設置幀,

timerBackground.frame = CGRectMake(0, 263, 100, 27);//or timerBackground.frame = CGRectMake(-2, 263, 102, 27); 

,並添加子視圖作爲,

[self.view addSubview:timerBackground]; 

在旁註中,請使用dateDay作爲變量名稱。

0

您的標籤視圖按鈕上面只是改變這樣

timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 27)]; 
[self.view addSubview:timerView]; 

    DateDay = [[UILabel alloc] initWithFrame:CGRectMake(6, 4, 20, 21)]; 
    DateDay.backgroundColor = [UIColor clearColor]; 
    DateDay.textColor = [UIColor whiteColor]; 
    [timerBackground addSubview:DateDay]; 

    UIButton *timerBackground = [UIButton buttonWithType:UIButtonTypeCustom]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"]forState:UIControlStateNormal]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"]forState:UIControlStateHighlighted]; 
[timerBackground addTarget:self action:@selector(openTimeChanger) forControlEvents:UIControlEventTouchUpInside]; 
    timerBackground.frame = CGRectMake(-2 , 15, 102, 27); 
    [timerView addSubview:timerBackground]; 
代碼