2

我已将 a 添加UIView到视图控制器的视图中。说:

CGRect paneRect = {10.0f, 10.0f, 300.0f, 50.0f};
UIView *pane = [[UIView alloc] initWithFrame:paneRect];
pane.backgroundColor = [UIColor greenColor];
pane.userInteractionEnabled = YES;
pane.clipsToBounds = NO;
[self.view addSubview:pane];

然后我添加了一个 UIButton 到pane

CGRect testRect = {10.0f, 25.0f, pane.frame.size.width - 20.0f, 50.0f};
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[test setTitle:@"test" forState:UIControlStateNormal];
[test setFrame:testRect];
[pane addSubview:test];

现在“测试”按钮的一半在窗格内,另一半不在。上半部分是交互式的,对触摸有反应,但下半部分不是。

在此处输入图像描述

是否有可能使整个“测试”按钮交互和可触摸而不是它的一半?

4

2 回答 2

0

就这样做

CGRect paneRect = {10.0f, 10.0f, 300.0f, 50.0f};
UIView *pane = [[UIView alloc] initWithFrame:paneRect];
pane.backgroundColor = [UIColor greenColor];
pane.userInteractionEnabled = NO;
pane.clipsToBounds = NO;
[self.view addSubview:pane];


CGRect testRect = {10.0f, 25.0f, pane.frame.size.width - 20.0f, 50.0f};
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[test setTitle:@"test" forState:UIControlStateNormal];
test.userInteractionEnabled = YES;
[test setFrame:testRect];
[pane addSubview:test];

对于 UIButton 的操作,只需添加

[test addTarget:self action:@selector(processButtonCLick) forControlEvents:UIControlEventTouchUpInside];

如果要在窗格的 uibutton 中心添加

 test.center =pane.center;
于 2013-01-20T06:53:16.070 回答
0

如果你真的想让这些观点相互一致,那么你可以做这样的事情

CGRect mainViewRect = {10.0f, 10.0f, 300.0f, 75.0f};
UIView *mainView = [[UIView alloc] initWithFrame:mainViewRect];
mainView.backgroundColor = [UIColor clearColor];
mainView.userInteractionEnabled = YES;
[self.view addSubview:mainView];

CGRect paneRect = {0.0f, 0.0f, 300.0f, 50.0f};
UIView *pane = [[UIView alloc] initWithFrame:paneRect];
pane.backgroundColor = [UIColor greenColor];
pane.userInteractionEnabled = YES;
pane.clipsToBounds = NO;
[mainView addSubview:pane];



CGRect testRect = {10.0f, 25.0f, pane.frame.size.width - 20.0f, 50.0f};
UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[test setTitle:@"test" forState:UIControlStateNormal];
[test setFrame:testRect];
[mainView addSubview:test];
于 2013-01-20T07:03:28.590 回答