关于UIView和UIButton的UIView不起作用

时间:2014-02-01 02:07:06

标签: ios uiview uibutton

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

按钮不响应触摸?为什么呢?

2 个答案:

答案 0 :(得分:2)

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将此self.view.bounds.size.height-60 background2与背景框架

进行比较

enter image description here

设置背景框self.view.bounds.size.height现在它将完美无缺

enter image description here

答案 1 :(得分:0)

您的代码运行正常,我只是尝试过。 我只将background2的框架更改为不在background的框架之外。