如何保持当前页面直到下一页完全加载(ios UIWebview)

时间:2015-10-08 11:49:22

标签: ios iphone uiwebview

我有一个基于UIWebview的应用, 当用户单击URL时,默认行为是立即显示空白页并等待页面加载

是否可以保留当前页面,直到加载下一页?

感谢您提前。

1 个答案:

答案 0 :(得分:0)

我很好奇这种功能的必要性,我可能同意Chandra Vaghasiyacomment

说过这是可能的。

  1. 让您的视图控制器成为UIWebView
  2. 的代表
  3. webViewDidStartLoad:方法中,拍摄Web视图的快照,并在视图层次结构中的Web视图上方添加结果视图。
  4. webViewDidFinishLoad:webView:didFailLoadWithError:方法中,只需隐藏快照视图。
  5. 示例代码:

    @interface ViewController () <UIWebViewDelegate>
    @property (weak, nonatomic) IBOutlet UIWebView *webView;
    @property (nonatomic, strong) UIView *snapshotView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        NSURL *url = [NSURL URLWithString:@"https://google.com"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:request];
    }
    
    - (void)webViewDidStartLoad:(UIWebView *)webView {
        self.snapshotView = [self.webView snapshotViewAfterScreenUpdates:NO];
    
        self.snapshotView.frame = self.webView.frame;
    
        [self.view insertSubview:self.snapshotView
                    aboveSubview:self.webView];
    
        self.snapshotView.hidden = NO;
    
        // THIS IS JUST FOR DEMONSTRATION PURPOSES SO
        // YOU CAN SEE THE LOADING HAPPENING BEHIND
        // THE SNAPSHOT VIEW
        self.snapshotView.alpha = 0.8;
    
        NSLog(@"Display snapshot");
    }
    
    - (void)webViewDidFinishLoad:(UIWebView *)webView {
        self.snapshotView.hidden = YES;
        NSLog(@"Hide snapshot");
    }
    
    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
        self.snapshotView.hidden = YES;
        NSLog(@"Hide snapshot due to error");
    }
    
    @end