Search code examples
javascriptiosobjective-cuiwebview

iOS UIWebView flicker issue


I have a UIWebView and a swipe gesture in the same screen. When I'm swiping, I am reloading the webview, because if I don't, the call to js is not being made.

The reload works only once, I am unable to swipe again.

When I removed [self.webview reload]swiping is done fine, but the UIWebView keeps flickering. It just jumps up everytime! I need to make a call to js everytime I swipe.

I tried these:

Method 1:

when loading webview:
 self.webview.alpha = 0;

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    [UIView beginAnimations:nil context:nil];    
    [UIView setAnimationDuration:0.30];    
    self.webview.alpha = 1;
    [UIView commitAnimations];
}

Method 2:

  - (void)webViewDidFinishLoad:(UIWebView *)webView {

    [self.webview setOpaque:NO];
    self.webview.backgroundColor = [UIColor clearColor];
  }

Method 3:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.webview loadHTMLString:@"<html><body style=\"background-color:black;\"></body></html>" baseURL:nil];    

    [self performSelector:@selector(loadWebview) withObject:nil afterDelay:0.1];
}
-(void)loadWebview {

    self.webview.delegate = self;
    self.webview.scrollView.delegate = self;
    self.webview.scrollView.bounces = NO;
    self.webview.scrollView.scrollsToTop = NO;
    [self.view addSubview:self.webview];
    pathToHtml = [[NSBundle mainBundle] pathForResource:@"mypath" ofType:@"html"];
    NSString* appHtml = [NSString stringWithContentsOfFile:pathToHtml encoding:NSUTF8StringEncoding error:nil];
    NSURL *baseURL = [NSURL fileURLWithPath:pathToHtml];

    [self.webview loadHTMLString:appHtml baseURL:baseURL]; 
}

Need help!


Solution

  • I found out that I had this in my webViewDidFinishLoad :

      //Make the page fit to view. THIS IS BAD
        CGRect webviewBound = self. webview.bounds;
        webviewBound.size.height = self. webview.scrollView.contentSize.height;
        self. webview.bounds = webviewBound;
    

    So I removed that and added:

      [self.webview.scrollView setContentSize: CGSizeMake(self.webview.frame.size.width, self.webview.scrollView.contentSize.height)];
        self.webview.scrollView.delegate = self;
    

    And I added this in viewDidLoad because I had grey background:

    //Removes the shadow from the webview i.e., "grey background"
    if ([[self.webview subviews] count] > 0)
    {
        for (UIView* shadowView in [[[self.webview subviews] objectAtIndex:0] subviews])
        {
            [shadowView setHidden:YES];
        }
    
        // unhide the last view so it is visible again because it has the content
        [[[[[self.webview subviews] objectAtIndex:0] subviews] lastObject] setHidden:NO];
    }