简体   繁体   English

带有UIButton的子视图不可单击

[英]Subview with UIButton isn't clickable

Here's the code I'm working with: 这是我正在使用的代码:

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];

Why is the mainButton clickable but the secondButton isn't? 为什么mainButton是可单击的,而secondButton不是可单击的?

K you should add sub view on self.view. 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];

try this. 尝试这个。 it will add two buttons on subview. 它将在子视图上添加两个按钮。

you should not add UIView as subview in UIButton 您不应该在UIButton中将UIView添加为子视图

TRY this 尝试这个

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];

EDIT: If you want to keep both buttons on different views then take two sub views and try like..... 编辑:如果您想将两个按钮保留在不同的视图上,则采取两个子视图,然后尝试.....

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];

The problem is that your subView is a child of the mainButton. 问题是您的subView是mainButton的子级。 You should add it to the viewController's view instead. 您应该将其添加到viewController的视图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM