IOS 设置UIViewController为背景半透明浮层弹窗,查看富文本图片详情

使用场景:UIViewController1 打开 UIViewController2(背景半透明弹窗)

案例:打开富文本网页<img>图片的url查看图片详情

WKWebView WKNavigationDelegate代理方法设置js代码点击事件

objectivec 复制代码
///注册添加图片标签点击js方法
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    ///js添加图片点击事件
    NSString *jsClickImage =@"function registerImageClickAction(){var imgs=document.getElementsByTagName('img');var length=imgs.length;for(var i=0; i < length;i++){img=imgs[i];if(\"ad\" ==img.getAttribute(\"flag\")){var parent = this.parentNode;if(parent.nodeName.toLowerCase() != \"a\")return;}img.onclick=function(){window.location.href='image-preview:'+this.src}}}";
    
    [webView evaluateJavaScript:jsClickImage completionHandler:nil];
    [webView evaluateJavaScript:@"registerImageClickAction()" completionHandler:nil];
    
    [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result,NSError *_Nullable error) {
        //获取webView内容高度
        CGFloat height = [result doubleValue];
//        [webView mas_updateConstraints:^(MASConstraintMaker *make) {
//              make.height.equalTo(@(height));
//        }];
        webView.frame = CGRectMake(0, 0, self.frame.size.width, height);
        [webView layoutIfNeeded];
        [webView setNeedsLayout];
        if (self.contentHeightCallback != nil) {
            self.contentHeightCallback(CGSizeMake(self.frame.size.width, height));
        }
    }];
    
}

///拦截点击图片
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    NSString *requestString = navigationAction.request.URL.absoluteString;
    NSString *imgKey = @"image-preview";
    if([requestString hasPrefix: imgKey]){
        NSString *url = [requestString substringFromIndex:imgKey.length+1];
        //查看图片详情
        [self showPicDetail:url];
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}

showPicDetail:() 方法打开浮窗:

objectivec 复制代码
-(void) showPicDetail:(NSString *)imgUrl{
    ShowBigPicDetailViewController *vc = [[ShowBigPicDetailViewController alloc] init];
    vc.imageUrl = imageUrl;
    //设置全屏显示   关键语句,必须有
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:vc animated:YES completion:nil];
}

设置UIViewController2 背景半透明

objectivec 复制代码
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置半透明背景
    [self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f]];
    ...
}

关闭浮窗弹窗:

objectivec 复制代码
[self dismissViewControllerAnimated:YES completion:nil];
相关推荐
问道飞鱼1 小时前
【移动端知识】移动端多 WebView 互访方案:Android、iOS 与鸿蒙实现
android·ios·harmonyos·多webview互访
DanB242 小时前
html复习
javascript·microsoft·html
mascon2 小时前
U3D打包IOS的自我总结
ios
名字不要太长 像我这样就好2 小时前
【iOS】继承链
macos·ios·cocoa
karshey3 小时前
【IOS webview】IOS13不支持svelte 样式嵌套
ios
潜龙95273 小时前
第4.3节 iOS App生成追溯关系
macos·ios·cocoa
游戏开发爱好者812 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
paid槮13 小时前
HTML5如何创建容器
前端·html·html5
拾光拾趣录14 小时前
HTML行内元素与块级元素
前端·css·html
神策技术社区19 小时前
iOS 全埋点点击事件采集白皮书
大数据·ios·app