SWIFT写的web view添加头部与尾部视图

释放双眼,带上耳机,听听看~!

webView的scrollView的子视图里的第0个视图,是真正控制web view的y值是从哪里开始的,只要改下它的frame就可以实现添加头视图,尾部视图需要计算加载的webview的高度。代码如下:


import UIKit
class WebViewController: UIViewController ,UIWebViewDelegate,UIScrollViewDelegate{
var webView:UIWebView!
var header:UIView!
var footer:UIView!
var webBrowserView:UIView!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
// Do any additional setup after loading the view.
webView = UIWebView()
webView.frame = CGRectMake(0, 0, SCREEN_W, SCREEN_H)
webView.delegate = self;
webView.backgroundColor = UIColor.clearColor()
self.view.addSubview(webView)
let str = "***************************************"
let request = NSURLRequest(URL: NSURL(string: str)!)
webView.loadRequest(request)
header = UIView()
header.frame = CGRectMake(0, 0, SCREEN_W, 200)
header.backgroundColor = UIColor.yellowColor()
webBrowserView = webView.scrollView.subviews[0]
var rect = self.webBrowserView.frame
rect.origin.y = CGRectGetMaxY(header.frame)
webBrowserView.frame = rect
//添加
webView.scrollView.addSubview(header)
webView.scrollView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
webView.scrollView.removeObserver(self, forKeyPath: "contentSize")
let views = self.view.viewWithTag(100)
let contentSize = webView.scrollView.contentSize
let vi = UIView()
vi.backgroundColor = UIColor.purpleColor()
vi.tag = 100
vi.frame = CGRectMake(0, contentSize.height, SCREEN_W, 150)
webView.scrollView.addSubview(vi)
webView.scrollView.contentSize = CGSizeMake(contentSize.width, contentSize.height+150)
webView.scrollView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.New, context: nil)
}

 

人已赞赏
iOS文章

iOS开发中遇到的问题

2020-5-14 3:48:06

iOS文章

swift error:Extra argument 'error' in call

2020-5-14 5:03:03

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索