在iOS中滚动Web视图而不使用导航控制器时隐藏导航栏

时间:2017-12-04 04:40:50

标签: ios objective-c uinavigationbar

I want the same behaviour as this

如果主视图有没有navigationController的导航栏,如何在Web视图中滚动时隐藏导航栏?导航栏不能通过故事板来检查“滑动时隐藏条形图”。

这不起作用

self.navigationController.hidesBarsOnSwipe = true

这就是我的SB的样子:

This is what my SB looks like:

此致

2 个答案:

答案 0 :(得分:1)

您可以尝试以下解决方案:

<强>夫特

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {
  @IBOutlet var webView:UIWebView!
  @IBOutlet var navigationBar:UINavigationBar!
  @IBOutlet var navigationBarTop:NSLayoutConstraint!
  var lastContentOffset:CGFloat = 0.0

  override func viewDidLoad() {
    super.viewDidLoad()
    self.webView.scrollView.delegate = self
    self.webView.loadRequest(URLRequest(url: URL(string: "https://www.apple.com")!))
  }

  public func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let threshold:CGFloat = 20.0
    let delta = abs(self.lastContentOffset - scrollView.contentOffset.y)
    if delta > threshold || scrollView.contentOffset.y <= 0 {
      if self.lastContentOffset > scrollView.contentOffset.y && self.navigationBarTop.constant < 0 && (self.lastContentOffset < (scrollView.contentSize.height - scrollView.frame.height)) {
        self.showNavBar(true)
      } else if (self.lastContentOffset < scrollView.contentOffset.y && self.navigationBarTop.constant == 0 && (self.lastContentOffset > 0)) {
        self.showNavBar(false)
      }
    }
    self.lastContentOffset = scrollView.contentOffset.y;
  }

  func showNavBar(_ isVisible:Bool) {
    UIView.animate(withDuration: 0.25, animations: {
      self.navigationBarTop.constant = isVisible ? 0 : -(self.navigationBar.frame.height + UIApplication.shared.statusBarFrame.height)
      self.view.setNeedsLayout()
      self.view.layoutIfNeeded()
    })
  }
}

完整的源代码:https://github.com/mugx/AnimatedNavigationBar

enter image description here

<强>目标C

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIScrollViewDelegate>
@property IBOutlet UIWebView *webView;
@property IBOutlet UINavigationBar *navigationBar;
@property IBOutlet NSLayoutConstraint *navigationBarTop;
@property (nonatomic,assign) CGFloat lastContentOffset;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView.scrollView.delegate = self;
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.apple.com"]]];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat threshold = 20.0;
    CGFloat delta = fabs(self.lastContentOffset - scrollView.contentOffset.y);
    if (delta > threshold || scrollView.contentOffset.y <= 0) {
        if (self.lastContentOffset > scrollView.contentOffset.y && self.navigationBarTop.constant < 0 && (self.lastContentOffset < (scrollView.contentSize.height - scrollView.frame.size.height))) {
            [self showNavBar:true];
        } else if (self.lastContentOffset < scrollView.contentOffset.y && self.navigationBarTop.constant == 0 && (self.lastContentOffset > 0)) {
            [self showNavBar:false];
        }
    }
    self.lastContentOffset = scrollView.contentOffset.y;
}

- (void)showNavBar:(Boolean)isVisible {
    [UIView animateWithDuration:0.25 animations:^{
        self.navigationBarTop.constant = isVisible ? 0 : -(self.navigationBar.frame.size.height + UIApplication.sharedApplication.statusBarFrame.size.height);
        [self.view setNeedsLayout];
        [self.view layoutIfNeeded];
    }];
}

@end

这是我认为的第一个解决方案,当然不是拖放,你必须链接相应的IBOutlet。无论如何,我认为机制很清楚,利用嵌入在webview中的scrollView委托,然后计算delta偏移量,以便显示/隐藏(使用流畅的动画)自定义导航栏。如果事情不明确,我会修改答案。

答案 1 :(得分:0)

 webView.scrollView.delegate = self



extension ViewController:UIScrollViewDelegate{
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        navigationController?.hidesBarsOnSwipe  = velocity.y > 0
    }
}