gpt4 book ai didi

ios - 如何使放置在 UIView 框架之外的 UIView 的 SubView 具有交互性?

转载 作者:行者123 更新时间:2023-12-01 16:52:39 26 4
gpt4 key购买 nike

我添加了一个 UIView到 View Controller 的 View 。说:

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

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

enter image description here

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

最佳答案

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

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

关于ios - 如何使放置在 UIView 框架之外的 UIView 的 SubView 具有交互性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14422323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com