如何在连接到UIWebView的导航栏上实现模糊的半透明效果?

时间:2014-11-14 14:00:39

标签: ios objective-c uiwebview uinavigationbar translucency

以下是设置:

  1. 背景图片。
  2. 导航栏。
  3. UIWebView。
  4. 我希望导航栏能够根据UIWebView的内容获得模糊的半透明效果,但它会根据背景图像变得模糊。我该如何解决?

    如何将导航栏连接到UIWebView,以便当用户向下滚动时,模糊效果会发生变化并正确显示?


    这是一个我真正有用的想法:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        [self setAutomaticallyAdjustsScrollViewInsets:YES];
    
        self.webView = [[WKWebView alloc] init];
        self.webView.frame = CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height);
        self.webView.scrollView.delegate = self;
    
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.insomnia.gr"]]];
    
        UINavigationBar *navigationBar = [[UINavigationBar alloc] init];
        navigationBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
        navigationBar.tintColor = [UIColor clearColor];
        navigationBar.translucent = YES;
    
        [self.view addSubview:self.webView];
        [self.view addSubview:navigationBar];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark -
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        NSLog(@"WebView - Location Y: %f Height: %f", self.webView.frame.origin.y, self.webView.frame.size.height);
    
        if (scrollView.contentOffset.y >= 44) {
    
            // Scale to maximum size.
            self.webView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        }
        else if (scrollView.contentOffset.y > 0) {
    
            // Scale relative to content offset.
            self.webView.frame = CGRectMake(0, 44 - scrollView.contentOffset.y, self.view.frame.size.width, self.view.frame.size.height + scrollView.contentOffset.y);
        }
        else {
    
            // Scale to original.
            self.webView.frame = CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height - 44);
        }
    }
    

    但是,它甚至不是一个优雅而简单的解决方案。

2 个答案:

答案 0 :(得分:2)

您需要在导航栏下移动网页视图,然后设置网络视图contentInset,如下所示:

navBar.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44);
webView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
webView.scrollView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
webView.scrollView.scrollIndicatorInsets = webView.scrollView.contentInset;

答案 1 :(得分:0)

转到xcode中的app设置,在状态栏样式中单击下拉菜单并选择“Light”。 这些应该适用于您的所有应用