https://github.com/potato512/SYSwiftLearning
// 实例化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.FlexibleBottommarginself.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: - uiwebviewdelegatefunc 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 = 1000let item02 = UIbarbuttonItem(Title: "start",action: Selector("buttonClick:"))item02.tag = 2000self.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 = truesegment.frame = CGRectMake(0.0,0.0,40.0)segment.addTarget(self,action: Selector("segmentValueChange:"),forControlEvents: UIControlEvents.ValueChanged)
// MARK: - segmentValueChangefunc 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("无效 *** 作") }}总结
以上是内存溢出为你收集整理的swift中UIWebView的使用全部内容,希望文章能够帮你解决swift中UIWebView的使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)