0

ここに私が取り組んでいるコードがあります:

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 はクリックできないのはなぜですか?

4

3 に答える 3

2

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

これを試して。サブビューに2つのボタンを追加します。

于 2013-05-14T12:14:23.477 に答える
0

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

編集:両方のボタンを異なるビューに保持したい場合は、2つのサブビューを取り、次のようにしてみてください.....

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];
于 2013-05-14T12:19:24.040 に答える
-1

問題は、subView が mainButton の子であることです。代わりに、viewController のビューに追加する必要があります。

于 2013-05-14T12:12:44.540 に答える