我在视图控制器的视图中添加了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];
然后我在pane
添加了一个UIButton:
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];
现在,“测试”按钮的一半在窗格内,另一半在外面。上半部分是交互式的,可以响应触摸,但下半部分不是。
是否有可能使整个“测试”按钮具有交互性和可触摸性,而不是它的一半?
答案 0 :(得分: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;
答案 1 :(得分: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];