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];
相关推荐
2501_916008892 小时前
iOS 发布全流程详解,从开发到上架的流程与跨平台使用 开心上架 发布实战
android·macos·ios·小程序·uni-app·cocoa·iphone
非专业程序员4 小时前
iOS/Swift:深入理解iOS CoreText API
ios·swift
某柚啊5 小时前
iOS移动端H5键盘弹出时页面布局异常和滚动解决方案
前端·javascript·css·ios·html5
RollingPin16 小时前
iOS八股文之 RunLoop
ios·多线程·卡顿·ios面试·runloop·ios保活·ios八股文
snow@li16 小时前
html5:拖放 / demo / 拖放事件(Drag Events)/ DataTransfer 对象方法
前端·html·拖放
2501_9160074717 小时前
iOS 混淆工具链实战,多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码混淆|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
LinXunFeng17 小时前
Flutter webview 崩溃率上升怎么办?我的分析与解决方案
flutter·ios·webview
游戏开发爱好者819 小时前
FTP 抓包分析实战,命令、被动主动模式要点、FTPS 与 SFTP 区别及真机取证流程
运维·服务器·网络·ios·小程序·uni-app·iphone
Dragonir20 小时前
React+Three.js 实现 Apple 2025 热成像 logo
前端·javascript·html·three.js·页面特效
aichitang202420 小时前
欧拉拓扑学公式:几何与拓扑的交汇
html·拓扑学