如何从UIWebView中删除渐变效果-即在顶部或底部超出滚动区域时看到的效果。
这段代码
webView.backgroundColor = [UIColor whiteColor];
只是改变了渐变的颜色,而没有移除它。如何才能实现移除?
(注意:这个问题与UIWebView underside不同)
如何从UIWebView中删除渐变效果-即在顶部或底部超出滚动区域时看到的效果。
这段代码
webView.backgroundColor = [UIColor whiteColor];
只是改变了渐变的颜色,而没有移除它。如何才能实现移除?
(注意:这个问题与UIWebView underside不同)
{
webView.backgroundColor = [UIColor whiteColor];
for (UIView* subView in [webView subviews])
{
if ([subView isKindOfClass:[UIScrollView class]]) {
for (UIView* shadowView in [subView subviews])
{
if ([shadowView isKindOfClass:[UIImageView class]]) {
[shadowView setHidden:YES];
}
}
}
}
}
webView.opaque = NO;
webView.backgroundColor = [UIColor clearColor];
for(UIView *view in webView.subviews){
if ([view isKindOfClass:[UIImageView class]]) {
// to transparent
[view removeFromSuperview];
}
if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView *sView = (UIScrollView *)view;
for (UIView* shadowView in [sView subviews]){
//to remove shadow
if ([shadowView isKindOfClass:[UIImageView class]]) {
[shadowView setHidden:YES];
}
}
}
}
隐藏滚动条指示器的方法如下:
for(UIView *aView in [[[webView subviews] objectAtIndex:0] subviews]) {
if([aView isKindOfClass:[UIImageView class]]) { aView.hidden = YES; }
}
它只是遍历UIWebView的子视图,并在其为图像视图时删除该视图。
我还没有将其放入任何App Store应用程序中,因此我不知道苹果是否会接受它。
编辑:Brian的链接提供了更多细节。
使用上面建议的方法后,您将无法稍后编辑滚动指示器/插图。它们也显示为UIImageView,因此您应该检查最后一个对象:
UIView* lastView = [[subView subviews] lastObject];
for (UIView* shadowView in [subView subviews])
{
if(shadowView!=lastView) ... <-this one is a scroll
}
我在 @damithH 的回答基础上进行了改进
@implementation UIWebView (Extensions)
- (void)setBackgroundAndShadowVisible:(BOOL)visible
{
self.opaque = !visible;
self.backgroundColor = [self.backgroundColor colorWithAlphaComponent:visible ? 1.0 : 0.0];
for(UIView *view in [self subviews])
{
if([view isKindOfClass:[UIImageView class]])
{
view.hidden = !visible;
}
if([view isKindOfClass:[UIScrollView class]])
{
UIScrollView *scrollView = (UIScrollView *)view;
for (UIView *shadowView in [scrollView subviews])
{
if ([shadowView isKindOfClass:[UIImageView class]])
{
shadowView.hidden = !visible;
}
}
}
}
}
@end
if (UIDevice.currentDevice.systemVersion.intValue < 7)
for (UIImageView *imageView in webView.scrollView.subviews)
if ([imageView isKindOfClass:[UIImageView class]] && imageView.image.size.width == 1)
imageView.hidden = YES;
我通过在WebView的scrollView顶部和底部添加白色子视图来实现这一点。我控制WebView的内容,所以我知道白色是可以的 - 如果您加载任意内容,则无法使用此方法。
// _topCover and _bottomCover are ivar UIViews
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// with cover views 300pt high, I couldn't scroll far enough to see the shadow,
// even in portrait on an iPad, which gives you the longest scroll distance
CGFloat coverage = 300;
_topCover = [[UIView alloc] initWithFrame:CGRectMake(0, -coverage, webView.bounds.size.width, coverage)];
_bottomCover = [[UIView alloc] initWithFrame:CGRectMake(0, webView.scrollView.contentSize.height, webView.bounds.size.width, coverage)];
_topCover.backgroundColor = [UIColor whiteColor];
_bottomCover.backgroundColor = [UIColor whiteColor];
// in case the webView is resized, e.g. by rotating the device
_topCover.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
_bottomCover.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[webView.scrollView addSubview:_topCover];
[webView.scrollView addSubview:_bottomCover];
}
我在页面加载后运行它,以便webView.scrollView.contentSize.height
会给我正确的高度。如果您的页面高度动态变化,我不确定这将如何工作。我的页面只加载一次;如果您的页面正在重新加载,则希望在第一次之后跳过对_topCover
和_bottomCover
进行alloc
/init
以提高效率。
更新:我不确定我上面使用的autoresizingMask
是否足够处理视图旋转。您可能需要将其放置在包含UIWebView
的UIViewController
中,在旋转后调整封面的大小:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
CGFloat coverage = 300;
_topCover.frame = CGRectMake(0, -coverage, self.webView.bounds.size.width, coverage);
_bottomCover.frame = CGRectMake(0, self.webView.scrollView.contentSize.height, self.webView.bounds.size.width, coverage);
}