【问题标题】:Is it possible to hide UINavigationController then show it when user swipe down?是否可以隐藏 UINavigationController 然后在用户向下滑动时显示它?
【发布时间】:2014-04-30 12:37:13
【问题描述】:

我想全屏显示,我正在隐藏系统栏和UINavigationBar,但我想让它重新出现在某些用户交互中,例如向下拖动内容或在顶部边缘附近按下等(更容易实现且更用户友好)。

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

编辑: 值得一提的是,这个全屏显示的视图是 UIWebView,而 UINavigationBar 在这个视图上被用来提供带有返回按钮的“返回”功能。

【问题讨论】:

    标签: ios uiwebview uinavigationcontroller show-hide


    【解决方案1】:

    我相信这是可能的。

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

    – scrollViewWillEndDragging:withVelocity:targetContentOffset:

    – scrollViewDidEndDragging:willDecelerate:

    然后只需显示/隐藏 UINavigationBar 之后

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


    如果上面的逻辑不起作用。你可以用UIView 伪造UINavigationBar。使用这些委托很容易显示和隐藏它。

    //编辑:如果你不想使用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];
    }
    

    完成! :)

    【讨论】:

    • 它似乎可以工作:) 但是以我使用 mac 和 Objective-c 的第 2 个月经验,它仍然像 abra-cadabra :) 我将更新我的问题,提到这个视图正在使用 UIWebView我想拥有所有空间,但可以使用 UINavigationBar 返回。
    • 你仍然可以按照我的建议去做。我会在几分钟内更新我的答案。
    • 我明天会试试这个:) 但完全按照你的建议,因为 UIWebView 已经有 UIScrollView - 所以我只使用那些滚动处理程序。
    • 我使用了- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate,当用户将if(scrollView.contentOffset.y < -32){}拖得足够远时,我会在UINavigationBar中进行动画处理。
    猜你喜欢
    • 2019-06-14
    • 2014-09-02
    • 2018-10-20
    • 2023-01-17
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    相关资源
    最近更新 更多