dlewis23
dlewis23

Reputation:

Remove UIWebView Shadow?

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

If its possible how do you do it?

Thanks

Upvotes: 31

Views: 21206

Answers (15)

Berlin Raj
Berlin Raj

Reputation: 124

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
        }
    }
}

Upvotes: 3

Nikolai Krill
Nikolai Krill

Reputation: 414

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];
}

Upvotes: 4

devney
devney

Reputation: 103

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.

Upvotes: 5

Friend_LGA
Friend_LGA

Reputation: 71

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;

Upvotes: 0

jodm
jodm

Reputation: 2617

This is a cleaner alternative to "Nikolai Krill" solution. This only hides UIImageViews within the UIWebView and not the UIWebBrowserView.

for (UIView *view in [[[webView subviews] objectAtIndex:0] subviews]) { 
  if ([view isKindOfClass:[UIImageView class]]) view.hidden = YES;
}   

Thanks James

Upvotes: 71

Kukosk
Kukosk

Reputation: 3012

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;
}

Upvotes: 1

denis2342
denis2342

Reputation: 520

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];
}

Upvotes: 14

rrrus
rrrus

Reputation: 323

Traverse all subviews, the UIImageViews whose image is only 1 pixel wide are shadow images, you can hide them.

- (void)hideShadows {
    [webview traverseViewsWithBlock:^(UIView *view) {
        UIImageView *imgView = ([view isKindOfClass:[UIImageView class]] ? (UIImageView*)view : nil;
        // image views whose image is 1px wide are shadow images, hide them
        if (imgView && imgView.image.size.width == 1) {
            imgView.hidden = YES;
        }
    }];
}

traverseViewsWithBlock does what it looks like:

- (void)traverseViewsWithBlock:(void (^)(UIView* view))block
{
    block(self);
    for (id subview in self.subviews) {
        [subview traverseViewsWithBlock:block];
    }
}

Upvotes: 2

damithH
damithH

Reputation: 5178

The easiest way to hide scroll indicators and transparent the web view here in UIWebView

To remove the scrolls.

    for(UIView *view in webView.subviews){   
         if ([view isKindOfClass:[UIScrollView class]]) {  
              UIScrollView *sView = (UIScrollView *)view;  
              //to hide verticalScroller  
              sView.showsVerticalScrollIndicator = NO;
              sView.showsVerticalScrollIndicator = NO;
         }   
    }  

Upvotes: 1

Jacob
Jacob

Reputation: 926

I added a recursive method as a category to the UIView object so that it will do a depth-first walk of the subviews of the method's receiving view, hiding any UIImageView subclasses it finds. It will not crash if there are no subviews. The -apply: method is from BlocksKit. You could rewrite this function not to use it, but the block is applied in parallel to each element of the receiving array, so it's pretty fast.

@implementation UIView (RemoveShadow)

- (void)removeShadow {
  if (self.subviews.count == 0 && [self isKindOfClass:[UIImageView class]]) {
    self.hidden = YES;
  } else if (self.subviews.count > 0) {
    [self.subviews apply:^(id sender) {
      [(UIView *)sender removeShadow];
    }];
  }
}

@end

Upvotes: 0

Geri Borb&#225;s
Geri Borb&#225;s

Reputation: 16598

You have to be careful, the scroll indicators are UIImageViews as well. I'll improve my code, but here's a basic subclassed solution:

http://forrst.com/posts/A_tiny_UIWebView_hack_remove_shadows_from_behi-gzH

Upvotes: 1

PfhorSlayer
PfhorSlayer

Reputation: 1366

There is a private method with the selector setAllowsRubberBanding: that takes a BOOL value. If passed NO, you will not be able to scroll the web view past the top or bottom of the content area, but will still let you scroll through the web view normally. Unfortunately, this method IS private, and your app will likely not be allowed onto the store if you use it.

You could, however, potentially try and extract the method implementation and bind it to a different selector that you've created, using the dynamic nature of Objective-C's runtime.

Still, the method is private and may no longer exist in future versions of the OS. If you still want to try, here's some sample code that will extract the setAllowsRubberBanding: method implementation and call it for you.

static inline void ShhhDoNotTellAppleAboutThis (UIWebView *webview)
{
    const char *hax3d = "frgNyybjfEhooreOnaqvat";
    char appleSelName[24];

    for (int i = 0; i < 22; ++i)
    {
        char c = hax3d[i];
        appleSelName[i] = (c >= 'a' && c <= 'z') ? ((c - 'a' + 13) % 26) + 'a' : ((c - 'A' + 13) % 26) + 'A';
    }
    appleSelName[22] = ':';
    appleSelName[23] = 0;

    SEL appleSEL = sel_getUid(appleSelName);

    UIScrollView *scrollView = (UIScrollView *)[webview.subviews objectAtIndex:0];
    Class cls = [scrollView class];
    if (class_respondsToSelector(cls, appleSEL) == NO)
    {
        return;
    }

    IMP func = class_getMethodImplementation(cls, appleSEL);
    func(scrollView, appleSEL, NO);
}

Please note that this will probably still get caught by Apple's static analyzer if you choose to submit an app using this code to the AppStore.

Upvotes: 5

Unfalkster
Unfalkster

Reputation: 683

I may be wrong, but I think the shadow only shows up when we scroll the webview doesn't it ? In that case, do you want to prevent the scrolling or really hide the shadow ? I don't know any tips that would hide the shadow. To disable the scrolling, I would setUserInteractionEnabled to NO.

Upvotes: 0

Michael Waterfall
Michael Waterfall

Reputation: 20569

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!

Upvotes: 0

user41806
user41806

Reputation: 559

I looked at the class properties and didn't find anything there but I can think of two "cover up" strategies:

1. You can use another view (parent of the web view) to clip the webview bounds.
2. You can add another view on top of the webview to cover the needed area with a color that matches the background, you can use an uiimage with a transparent area in the center.

By the way I don't like this standard background of the table views :P, but changing it can be a pain in the ass :P

Upvotes: 1

Related Questions