swift中UIWebView的使用
2016-10-20 18:00
211 查看
https://github.com/potato512/SYSwiftLearning
![](http://img.blog.csdn.net/20161020175738510?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
// 实例化
self.webview = UIWebView(frame: CGRectMake(0.0, CGRectGetHeight(segment.frame), CGRectGetWidth(self.view.bounds), (CGRectGetHeight(self.view.bounds) - CGRectGetHeight(segment.bounds))))
self.view.addSubview(self.webview!)
self.webview!.backgroundColor = UIColor.clearColor()
// 使用"|"无效,应改成[xx,xx,..]
// self.webview!.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleBottomMargin
self.webview!.autoresizingMask = [UIViewAutoresizing.FlexibleHeight, UIViewAutoresizing.FlexibleBottomMargin]
self.webview!.scrollView.scrollEnabled = true
// 设置代理对象(注意添加协议及实现代理方法)
self.webview!.delegate = self
// 实例化
self.webview = UIWebView(frame: CGRectMake(0.0, CGRectGetHeight(segment.frame), CGRectGetWidth(self.view.bounds), (CGRectGetHeight(self.view.bounds) - CGRectGetHeight(segment.bounds))))
self.view.addSubview(self.webview!)
self.webview!.backgroundColor = UIColor.clearColor()
// 使用"|"无效,应改成[xx,xx,..]
// self.webview!.autoresizingMask = UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleBottomMargin
self.webview!.autoresizingMask = [UIViewAutoresizing.FlexibleHeight, UIViewAutoresizing.FlexibleBottomMargin]
self.webview!.scrollView.scrollEnabled = true
// 设置代理对象(注意添加协议及实现代理方法)
self.webview!.delegate = self
// 释放,内存管理 deinit { self.webview?.delegate = nil self.webview?.loadHTMLString("", baseURL: nil) self.webview?.stopLoading() self.webview?.removeFromSuperview() self.webview = nil NSURLCache.sharedURLCache().removeAllCachedResponses() UIApplication.sharedApplication().networkActivityIndicatorVisible = false }
// MARK: - UIWebViewDelegate func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { print("\(request),\(navigationType)") // 用于控制子链接是否可以打开跳转 UIApplication.sharedApplication().networkActivityIndicatorVisible = true return true } func webViewDidStartLoad(webView: UIWebView) { // 加载开始 print("1 webViewDidStartLoad") UIApplication.sharedApplication().networkActivityIndicatorVisible = true } func webViewDidFinishLoad(webView: UIWebView) { // 加载结束(加载成功) print("2 webViewDidFinishLoad") UIApplication.sharedApplication().networkActivityIndicatorVisible = false } func webView(webView: UIWebView, didFailLoadWithError error: NSError?) { // 加载结束(加载失败) print("3 webView error = \(error)") UIApplication.sharedApplication().networkActivityIndicatorVisible = false }
let item01 = UIBarButtonItem(title: "stop", style: UIBarButtonItemStyle.Done, target: self, action: Selector("buttonClick:")) item01.tag = 1000 let item02 = UIBarButtonItem(title: "start", style: UIBarButtonItemStyle.Done, target: self, action: Selector("buttonClick:")) item02.tag = 2000 self.navigationItem.rightBarButtonItems = [item01, item02]
// MARK: - 响应事件 func buttonClick(sender:UIBarButtonItem) { let index = sender.tag if 1000 == index { // 停止 if (self.webview != nil) { if (self.webview!.loading) { self.webview!.stopLoading() UIApplication.sharedApplication().networkActivityIndicatorVisible = false } } } else if 2000 == index { // 开始 if (self.webview != nil) { if (!self.webview!.loading) { let request = NSURLRequest(URL: self.url) self.webview!.loadRequest(request) UIApplication.sharedApplication().networkActivityIndicatorVisible = true } } } }
// 多按钮视图控件(控制webview的后退,前进,或重新加载属性) let segment = UISegmentedControl(items: ["GoBack","GoForward","Reload"]) self.view.addSubview(segment) segment.momentary = true segment.frame = CGRectMake(0.0, 0.0, CGRectGetWidth(self.view.bounds), 40.0) segment.addTarget(self, action: Selector("segmentValueChange:"), forControlEvents: UIControlEvents.ValueChanged)
// MARK: - segmentValueChange func segmentValueChange(sender:UISegmentedControl) { let index = sender.selectedSegmentIndex; switch (index) { case 0: let isGoBack = self.webview!.canGoBack if isGoBack { self.webview?.goBack() } case 1: let isGoForward = self.webview!.canGoForward if isGoForward { self.webview?.goForward() } case 2: let isReload = self.webview!.loading if !isReload { self.webview?.reload() UIApplication.sharedApplication().networkActivityIndicatorVisible = true } default : print("无效操作") } }
相关文章推荐
- iOS基础网络教程-Swift版本: 3.使用UIWebView加载本地的数据
- Swift UIWebView的使用
- swift学习第八天 项目实战-知乎日报之UIWebView使用
- Swift - 使用UIWebView和UIToolbar制作一个浏览器
- Swift - 使用UIWebView和UIToolbar制作一个浏览器
- swift之UIWebview的使用
- iOS基础网络教程-Swift版本: 2.使用UIWebView请求数据
- swift——uiwebview的使用
- [iOS] 使用UIWebView时objective-c与javascript互相调用 1
- ios开发使用UIWebView显示多种文档
- ios 编程: ipad 和 iphone开发中使用 UIWebView 来播放视频的时候,在页面切换时 停止播放
- 不使用IB在UIWebView里面扩展添加UIMenuItem菜单
- cocos2d-x 使用UIWebView加载网页
- UIWebView的使用
- iphone开发学习,开源之SVWebViewController,UIWebView支持后退、前进、刷新、使用Safari打开
- iphone UITableView及UIWebView的使用
- UIWebView 加载网页时使用程序中的背景
- 使用Keystone认证多节点安装Swift
- UIWebView 使用
- UIWebView 使用方法