[ios]UIButton 在子视图的滚动视图不工作

发布时间: 2017/3/21 23:19:57
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我的问题是,我的按钮不起作用时 im 分页到我子视图。该按钮位于上滚动,一切工作正常。

主要 ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.sView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.sView.contentSize = CGSizeMake(self.sView.frame.size.width,
                                    self.sView.frame.size.height*2);
self.sView.pagingEnabled=YES;
self.sView.backgroundColor = [UIColor redColor];
self.sView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.sView];

self.sVC = [[StartViewController alloc] initWithNibName:nil bundle:nil];
self.sVC.view.frame = CGRectMake(0, 0, self.sView.frame.size.width, self.sView.frame.size.height);

[self.sView addSubview:self.sVC.view];

[self addChildViewController:self.sVC];
}

子 ViewController

 - (void)viewDidLoad
{
[super viewDidLoad];

self.playView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
self.playView.backgroundColor = [UIColor greenColor];

[self.view addSubview:self.playView];

UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
btn.backgroundColor = [UIColor whiteColor];
[btn addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];

[btn setUserInteractionEnabled:YES];

[self.playView addSubview:btn];
}

- (void)show:(id)sender
{
NSLog(@"--test");
}

解决方法 1:

嗯,主要的问题是,你的按钮是其视图控制器视图框架之外。

您的视图控制器显然是 (0,0,320,screenHeight) 568 或 480 screenHeight 在哪里。这是默认行为,您可能永远不应该更改它。 将工作事件,只有当它发生在视图控制器框架内部。

您的代码的问题是,您已经定义了您的 playview 框架为 (0,screenHeight,320,screenHeight)。所以这个 playview 是在哪里没有事件可以抓住的 viewcontroller 视图下方。因为你的按钮是 playview 里面它不是在 viewcontroller 以及和它不会工作。

你应该通过设置其框架 (0,0,320,screenHeight),以使事件将被逮设置视图控制器里面你 playview。然后,当你将它添加到其父,您可以更改您的视图控制器的整个框架。

在 SubviewController:

 - (void)viewDidLoad
{
    [super viewDidLoad];

    self.playView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.playView.backgroundColor = [UIColor greenColor];

    [self.view addSubview:self.playView];

    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
    btn.backgroundColor = [UIColor whiteColor];
    [btn addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];

    [btn setUserInteractionEnabled:YES];

    [self.playView addSubview:btn];
}

- (void)show:(id)sender
{
    NSLog(@"--test");
}

在您的 MainView 控制器︰

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.sView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.sView.contentSize = CGSizeMake(self.sView.frame.size.width,
                                self.sView.frame.size.height*2);
    self.sView.pagingEnabled=YES;
    self.sView.backgroundColor = [UIColor redColor];
    self.sView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:self.sView];

    self.sVC = [[StartViewController alloc] initWithNibName:nil bundle:nil];
    self.sVC.view.frame = CGRectMake(0, _sView.frame.size.height, _sView.frame.size.width, _sView.frame.size.height);

    [self.sView addSubview:self.sVC.view];
    [self addChildViewController:self.sVC];
}
赞助商