是否可以隐藏UINavigationController然后在用户向下滑动时显示它?

时间:2014-04-30 12:37:14

标签: ios uiwebview uinavigationcontroller show-hide

我认为我想拥有全屏,我正在隐藏系统栏和UINavigationBar,但我想让它重新出现在某些用户互动上,例如拖动内容或按下顶边等(这更容易实现,更友好)。

到目前为止,我的搜索没有成功,因此我对SO的问题。

编辑: 值得一提的是,这个全屏显示的视图是UIWebView,UINavigationBar用于此视图,通过后退按钮提供“返回”功能。

1 个答案:

答案 0 :(得分:2)

我相信它是可能的。

如果您想要一些简单的用户交互,例如向下拖动,那么您需要使用UIScrollView并实现其委托。之后,您可以使用其中一些方法

– scrollViewWillEndDragging:withVelocity:targetContentOffset:

– scrollViewDidEndDragging:willDecelerate:

然后只显示/隐藏UINavigationBar

[self.navigationController setNavigationBarHidden:YES/NO animated:YES];


如果上述逻辑不起作用。您可以使用UINavigationBar伪造UIView。这些代表很容易展示和隐藏它。

//编辑:如果您不想使用UIScrollView,可以使用UIPanGestureRecognizer并检查手势是否向上/向下拖动。但是,我认为使用UIScrollView会更容易。


//更新: 您仍然可以使用UIScrollViewUIWebView。喜欢这个

-(void)viewDidLoad
{
    //init an UIScrollView and UIWebView
    [self.view addSubview:theScrollView];
    [theScrollView addSubview:theWebView]; 

    //back button
    UIButton *back = [[UIButton alloc] init];
    //custom button: set its frame, title, image ...

    //set action for button
    [back addTarget:self action:@selector(backAction:) forEvent:UIControlEventTouchUpInside];
    //add to view
    [theWebView addSubView: back]; //or [self.view addSubView:back]; --both will work just fine

}

-(void)backAction:(UIButton *)sender
{
    //if you use navigation push, use this
    [self.navigationController popViewControllerAnimated:YES];
    //if you use navigation modal, add this line of code to its previous view controller
    [self.navigationController dismissViewControllerAnimated: YES completion: nil];
}

完成! :)