簡體   English   中英

關於UIView和UIButton的UIView不起作用

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

按鈕不響應觸摸? 為什么?

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與背景幀的幀進行比較

在此輸入圖像描述

設置你的背景框架self.view.bounds.size.height現在它將完美地工作

在此輸入圖像描述

你的代碼工作正常,我只是嘗試過。 我只改變了幀background2為不是的框架的外側background

暫無
暫無

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

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