简体   繁体   English

透明UINavigationBar下的UIWebView

[英]UIWebView under transparent UINavigationBar

I have a UIWebView which I want to put under my translucent UINavigationBar. 我有一个UIWebView,我想放在我的半透明UINavigationBar下面。 Normally when I put a UIScrollView under a translucent UINavigationBar, I set its contentOffset such that all content will be initially pushed after the bar so that it can be seen; 通常,当我将UIScrollView置于半透明的UINavigationBar下时,我设置了它的contentOffset,以便所有内容最初都会在条形图之后被推送,以便可以看到它; thereafter, the user can scroll text and it will underlap the bar. 此后,用户可以滚动文本,它将重叠条。

The problem is that UIWebView appears not to be a proper subclass of UIScrollView; 问题是UIWebView似乎不是UIScrollView的合适子类; thus, I can't use setContentOffset. 因此,我不能使用setContentOffset。 Does anyone have any tips or tricks on getting a UIWebView to look good with a translucent navigation bar? 有没有人有任何提示或技巧让UIWebView看起来很好用半透明的导航栏? Thanks. 谢谢。

As of iOS 5 you can use the scrollView property of UIWebView, and set a contentInset to adjust the positon. 从iOS 5开始,您可以使用UIWebView的scrollView属性,并设置contentInset来调整位置。

CGFloat top = 0.0;

if( self.navigationController != nil && self.navigationController.navigationBar.translucent ) {
    top = self.navigationController.navigationBar.bounds.size.height;
}

UIWebView* wv = ...

wv.scrollView.contentInset = UIEdgeInsetsMake(top, 0.0, 0.0, 0.0);

The easiest way is to set: 最简单的方法是设置:

webView.clipsToBounds = NO;
webView.scrollView.clipsToBounds = NO;

It work on bouth iOS 7 & 6, and I didn't try, but on iOS 5 probably too 它可以在iOS 7和6上运行,我没试过,但也可能在iOS 5上运行

I achieved this by setting the UIWebView subviews to not clip to bounds, then I could put a toolbar on top of it and get the desired effect: 我通过将UIWebView子视图设置为不剪切到边界来实现这一点,然后我可以在其上面放置一个工具栏并获得所需的效果:

for (UIView *subview in theWebView.subviews) {
    subview.clipsToBounds = NO;
}

I am not sure if there is a clean method to doing this. 我不确定是否有一个干净的方法来做到这一点。 I have not tried it, but here is an idea: 我没试过,但这是一个想法:

  1. draw the uiwebview at origin 0,0 在原点0,0绘制uiwebview
  2. intercept all screen touches 拦截所有屏幕触摸
  3. if you detect the user dragging up (scrolling up the webview), dont pass the touch on 如果您检测到用户向上拖动(向上滚动webview),请不要通过触摸
  4. Instead, move the webview up x pixels so its origin is (0,-x). 相反,将webview向上移动x像素,使其原点为(0,-x)。 Then change the height to add x pixels 然后更改高度以添加x像素
  5. Youy will need to keep some sort of state so that you know when to stop resizing the webview and start passing on the touches so the webview will scroll. 你需要保持某种状态,以便你知道何时停止调整webview的大小并开始传递触摸,以便webview将滚动。

Getting it back is the same only you do it on a drag down (scrolling down the webview). 只需在向下拖动(向下滚动webview)时,将其取回即可。

Another way to possibly do this is to insert a div in the html code you are rendering so that it is spaced out up on the top of the page. 另一种可能的方法是在您正在渲染的html代码中插入一个div,使其在页面顶部间隔开。 make sure you set your zoom correctly and set the uiwebviews origin to (0, -x) to begin with. 确保正确设置缩放并将uiwebviews原点设置为(0,-x)开头。

With the advent of iOS 7, the offset height would now need to include the height of the top status area. 随着iOS 7的出现,偏移高度现在需要包括顶部状态区域的高度。 Otherwise, iOS7 devices will have 20 pixels of webview still hidden under the navigation bar. 否则,iOS7设备将在导航栏下隐藏20个像素的webview。

In a project that needs to support iOS 7 and older devices, a macro like the one found here: 在需要支持iOS 7和旧设备的项目中,可以使用如下所示的宏

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

Can be very useful. 可以非常有用。 A modified version of zerotool's helpful code listed above could now look something like this: 上面列出的zerotool有用代码的修改版现在看起来像这样:

if (self.navigationController != nil && self.navigationController.navigationBar.translucent) {
    top = self.navigationController.navigationBar.bounds.size.height;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        top += [[UIScreen mainScreen] applicationFrame].origin.y;
    }
}
// (etc.)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM