Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios ScrollView子视图中的UIButton不工作_Ios_Iphone_Objective C_Uiscrollview_Uibutton - Fatal编程技术网

Ios ScrollView子视图中的UIButton不工作

Ios ScrollView子视图中的UIButton不工作,ios,iphone,objective-c,uiscrollview,uibutton,Ios,Iphone,Objective C,Uiscrollview,Uibutton,我的问题是,当我调到我的子视图时,我的按钮不起作用。按钮是否位于ScrollView上?一切正常 主视图控制器 - (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 = C

我的问题是,当我调到我的子视图时,我的按钮不起作用。按钮是否位于ScrollView上?一切正常

主视图控制器

- (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];
}
子视图控制器

 - (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");
}

您需要启用父scrollview的用户交互

[_sView setUserInteractionEnabled:YES];

好的,主要问题是你的按钮在它的视图控制器视图框之外

您的视图控制器显然是(0、0320、屏幕高度),其中屏幕高度是568或480。这是默认行为,您可能永远不应该更改它。 事件只有在视图控制器的框架内发生时才有效。

代码的问题在于,您已将playview帧定义为(0,屏幕高度,320,屏幕高度)。因此,此播放视图位于viewcontroller视图下方,无法捕捉任何事件。由于您的按钮位于playview中,因此它也不在viewcontroller中,因此无法工作

您应该做的是通过设置其帧(0,0320,屏幕高度)在视图控制器内设置playview,以便捕捉事件。然后,可以在将视图控制器添加到其父视图控制器时更改其整个帧

在子视图控制器中:

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

只是想了解一下,您是否正在将SubViewController的视图作为子视图添加到MainViewController中?SubViewController的类实际上是StartViewController吗?正确,我的主视图是一个scrollview,当我向下滚动时,我会看到带有按钮的子视图。在父scrollview上,交互正在工作(我测试了它)。在subViewController上启用交互不会影响问题。好吧,这对我来说很有效,你更改了框架了吗?它是崩溃了还是什么都没发生?你必须给出更多的细节,而不仅仅是“它不工作”视图是什么?滚动视图?不工作不意味着什么都不会发生吗?我在示例中就是这样做的。我正在MainViewController中创建一个滚动视图(带分页)。使用一个屏幕的边距,我将SubViewController添加到ScrollView(当您垂直分页一次时)。在该子视图中有一个按钮,我可以看到但不能单击。很抱歉,我不明白您到目前为止尝试了什么。你注意到镜框的变化了吗?我上面写的代码对我来说非常好(在页面滚动视图中)。通过复制代码,我确实遇到了与您相同的问题,它无法工作的原因是UIButton位于viewcontroller框架之外。我在顶部更改了代码。这就是你的意思吗?使用此代码时,我的按钮也不起作用。