簡體   English   中英

帶有UIButton的子視圖不可單擊

[英]Subview with UIButton isn't clickable

這是我正在使用的代碼:

UIButton *mainButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[mainButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal];
mainButton.center = CGPointMake(self.view.frame.size.width/2.0f, self.view.frame.size.height/2.0f);
[self.view addSubview:mainButton];

UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 200, 100)];
[subView setBackgroundColor:[UIColor purpleColor]];
[mainButton addSubview:subView];

UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[secondButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal];
secondButton.center = CGPointMake(subView.frame.size.width/2.0f, subView.frame.size.height/2.0f);
[subView addSubview:secondButton];

為什么mainButton是可單擊的,而secondButton不是可單擊的?

K,您應該在self.view上添加子視圖。

UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 200, 100)];
[subView setBackgroundColor:[UIColor purpleColor]];
[self.view addSubview:subView];

UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[secondButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal];
secondButton.center = CGPointMake(subView.frame.size.width/2.0f, subView.frame.size.height/2.0f);
[subView addSubview:secondButton];
UIButton *thirdButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 0, 100, 100)];
[secondButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal];
secondButton.center = CGPointMake(subView.frame.size.width/2.0f, subView.frame.size.height/2.0f);
[subView addSubview:secondButton];

嘗試這個。 它將在子視圖上添加兩個按鈕。

您不應該在UIButton中將UIView添加為子視圖

嘗試這個

UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 200, 100)];
[subView setBackgroundColor:[UIColor purpleColor]];
[self.view addSubview:subView];

UIButton *mainButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[mainButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal];
mainButton.center = CGPointMake(self.view.frame.size.width/2.0f, self.view.frame.size.height/2.0f);
[subView addSubview:mainButton];

UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[secondButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal];
[subView addSubview:secondButton];

編輯:如果您想將兩個按鈕保留在不同的視圖上,則采取兩個子視圖,然后嘗試.....

UIView *subView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[subView1 setBackgroundColor:[UIColor purpleColor]];
[self.view addSubview:subView1];

UIButton *mainButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[mainButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal];
[subView1 addSubview:mainButton];


UIView *subView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 100, 100)];
[subView2 setBackgroundColor:[UIColor purpleColor]];
[self.view addSubview:subView2];


UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[secondButton setImage:[UIImage imageNamed:@"CircleShape.png"] forState:UIControlStateNormal];
[subView2 addSubview:secondButton];

問題是您的subView是mainButton的子級。 您應該將其添加到viewController的視圖中。

暫無
暫無

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

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