简体   繁体   中英

UIView on UIView and UIButton not work

UIView *background = [[UIView alloc] initWithFrame:self.view.bounds];
background.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-70);
[self.view addSubview:background];

UIView *background2 = [[UIView alloc] initWithFrame:CGRectMake(10, self.view.bounds.size.height-60, 300, 50)];
background2.backgroundColor = [UIColor whiteColor];
background2.layer.cornerRadius = 8;
background2.layer.masksToBounds = YES;
[background addSubview:background2];


UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 158, 50);
[button setTitle:@"myButton" forState:UIControlStateNormal];
[button addTarget:self action:@selector(myButton:) forControlEvents:UIControlEventTouchUpInside];
[background2 addSubview:button];

button does not respond to touch? Why?

background.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-70);


 UIView *background2 = [[UIView alloc] initWithFrame:CGRectMake(10, self.view.bounds.size.height-60, 300, 50)];

self.view.bounds.size.height-70 compare this self.view.bounds.size.height-60 background2 out of frame of background frame

在此输入图像描述

set your background frame self.view.bounds.size.height now it will perfectly works

在此输入图像描述

Your code works fine, I just tried it. I only changed the frame of background2 to not be outside of the frame of background .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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