Remove UIWebView Shadow?

后端 未结 15 1170
慢半拍i
慢半拍i 2020-12-02 11:12

Does anyone know if its possible to remove the shadow that is placed on the UIWebView window?

Example: http://uploadingit.com/files/1173105_olub5/shadow.png

相关标签:
15条回答
  • 2020-12-02 11:53

    Here is a Swift function that gets rid of the shadow in a UIWebView in iOS 9. It’s safer than any alternative I’ve seen on SO because everything in it is in Apple documentation, and it specifically alters the shadow property (as opposed to hiding the entire view or some other property of the view).

    func removeShadow(webView: UIWebView) {
        for subview:UIView in webView.scrollView.subviews {
            subview.layer.shadowOpacity = 0
            for subsubview in subview.subviews {
                subsubview.layer.shadowOpacity = 0
            }
        }
    }
    

    You can always access the subviews property of a UIView(documentation). Every UIView has a layer property that is a CALayer (documentation). Every CALayer has shadowOpacity (documentation).

    Caveats:

    1. You might have to go deeper in navigating the view hierarchy through subviews depending on your situation.
    2. This works as long as you don’t want any shadows anywhere in the web view controller. If you have a view where you want to keep the shadow (other than the default UIWebView shadow), then you could add an if-check to identify that view and not set that view’s layer’s shadowOpacity to zero.
    3. According to Apple “For complex views declared in UIKit and other system frameworks, any subviews of the view are generally considered private and subject to change at any time. Therefore, you should not attempt to retrieve or modify subviews for these types of system-supplied views. If you do, your code may break during a future system update” . . . in other words, UIWebView can change and its not recommended to be digging into these subviews. However, digging into the UIWebView is the only way to get rid of the shadow and this is a relatively safe way to do it.
    0 讨论(0)
  • 2020-12-02 11:54

    the small for loop is very dangerous because it can crash if apple changes the number of the subviews.

    this way it does at least not crash when something changes:

    if ([[webView subviews] count] > 0)
    {
        for (UIView* shadowView in [[[webView subviews] objectAtIndex:0] subviews])
        {
            [shadowView setHidden:YES];
        }
    
        // unhide the last view so it is visible again because it has the content
        [[[[[webView subviews] objectAtIndex:0] subviews] lastObject] setHidden:NO];
    }
    
    0 讨论(0)
  • 2020-12-02 11:55

    This can be done without use of private APIs. All you need to do is hide each UIImageView with the shadow in it. Heres the code:

    for (int x = 0; x < 10; ++x) {
        [[[[[webView subviews] objectAtIndex:0] subviews] objectAtIndex:x] setHidden:YES];
    }
    
    0 讨论(0)
  • 2020-12-02 12:00

    What about a category on UIWebView like this:

    - (BOOL)showsScrollShadows
    {
        for(UIImageView *imageView in [self imageViewsWithShadows])
        {
            if(imageView.hidden)
            {
                return NO;
            }
    
            break;
        }
    
        return YES;
    }
    
    - (void)setShowsScrollShadows:(BOOL)showsScrollShadows
    {
        [[self imageViewsWithShadows] makeObjectsPerformSelector:@selector(setHidden:) withObject:@(!showsScrollShadows)];
    }
    
    - (NSArray *)imageViewsWithShadows
    {
        NSArray *potentialShadowImageViews = (self.subviews.count > 0) ? [self.subviews[0] subviews] : nil;
    
        if(potentialShadowImageViews.count > 0)
        {
            NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings)
            {
                return [evaluatedObject isKindOfClass:[UIImageView class]];
            }];
    
            return [potentialShadowImageViews filteredArrayUsingPredicate:predicate];
        }
    
        return nil;
    }
    
    0 讨论(0)
  • I've had a look around and can't see anything related to it. Apart from masking it with a view or clipping it somehow, the only thing I can think of is to loop through all of the UIWebView subviews (and sub-subviews etc.) and see if you can see anything there!

    0 讨论(0)
  • 2020-12-02 12:01

    Try this

    func webViewDidFinishLoad(_ webView: UIWebView) {
        for shadowView in self.webView.scrollView.subviews {
            if !shadowView.isKind(of: UIImageView.self) {
                shadowView.subviews[0].layer.shadowColor = UIColor.clear.cgColor
            } else {
                shadowView.layer.shadowColor = UIColor.clear.cgColor
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题