目标C:子视图不可点击

时间:2012-03-29 09:05:47

标签: iphone objective-c ios view

我即将为iPhone应用程序实现类似Facebook的侧面导航。我可以毫无问题地滑入和滑出但是当我想点击某个按钮或导航视图中的任何内容时没有任何反应。

以下是我在viewDidLoad中初始化的侧面导航视图的代码:

self.sideNavigationView = [[UIView alloc] initWithFrame:CGRectMake(self.navigationController.view.frame.size.width, 20, 238, self.navigationController.view.frame.size.height-20)];
[self.navigationController.view addSubview:self.sideNavigationView];

然后我有以下方法将其滑入:

- (IBAction) openMenuBar: (id) sender {       
if (!self.sideNavigationIsExpanded) {
    self.sideNavigationView.frame = CGRectMake(self.navigationController.view.frame.size.width, 20, 238, self.navigationController.view.frame.size.height-20);

}
CGRect destination = self.navigationController.view.frame;
CGRect sideNavigationDestination = self.sideNavigationView.frame;

sideNavigationDestination.size.width = 238;


// Slide back
if (self.sideNavigationIsExpanded) {       
    sideNavigationDestination.origin.x = self.view.frame.size.width;
    destination.origin.x = 0;
    sideNavigationDestination.origin.y = 20;
    self.sideNavigationIsExpanded = NO;
} 
// Slide in
else {
    sideNavigationDestination.origin.x = self.view.frame.size.width;
    destination.origin.x = - sideNavigationDestination.size.width;            

    sideNavigationDestination.origin.y = 20;
    self.sideNavigationIsExpanded = YES;

    self.sideNavigationView.hidden = NO;
}


[UIView animateWithDuration:0.25 animations:^{

    self.sideNavigationView.frame = sideNavigationDestination;
    self.navigationController.view.frame = destination;        


} completion:^(BOOL finished) {
    if (!self.sideNavigationIsExpanded)
        self.sideNavigationView.hidden = YES;
    self.view.userInteractionEnabled = !self.sideNavigationIsExpanded;
    self.sideNavigationView.userInteractionEnabled = self.sideNavigationIsExpanded;        
}];

    }

我在每个子视图中尝试了几个userInteractionEnabled = YES的组合......没有改变...... 可能是我的sideNavigationView的子视图不可选,因为当我初始化它时它是不可见的?我尝试用一​​个框架(CGRectMake(0,0,238,300))初始化它并且这很有效。 -_- 那么我怎么能解决我的问题呢?

1 个答案:

答案 0 :(得分:0)

由于sideNavigationView是self.view的子视图,因此这一行:

self.view.userInteractionEnabled = !self.sideNavigationIsExpanded;

阻止触摸到达你的sideNavigationView。如果自己的userInteractionEnabled属性设置为NO,则视图不会将触摸传递给其子视图,即使您已在下一行中将子视图的属性设置为YES。

至于屏幕外的初始化,我不认为这会影响视图在屏幕上移回后是否能够接收到触摸。