我想添加一个标题视图,UIWebView
类似于 MobileSafari 中的地址/搜索栏和 Sophia Teutschler 的优秀 Articles.app。更准确地说,我想在 UIWebView 上方创建一个“拉动固定方向”视图,就像在文章中一样。文章确实使用了 UIWebView,所以这似乎是可能的。如本文所述,有没有一种方法可以实现这一点,而不必一直将其嵌入UIWebView
到 a中UIScrollView
并更新其大小?显然,我确实需要滚动事件来使“拉动以固定方向”行为相应。
7 回答
经过几次尝试,我决定采用这个解决方案:
基本上我刚刚将我的标题 UIView 添加为 webview.scrollview 子视图。
为避免标题与浏览器视图重叠:
- 循环浏览所有 [webview.scrollView 子视图]
- 寻找最大的,应该包含浏览器(其他用于阴影和线条)
改变它的起源.y
这是代码:
CGRect browserCanvas = web.bounds; for(int i=0; i< [[web.scrollView subviews] count]; i++) { UIView* subview = [[web.scrollView subviews] objectAtIndex:i]; CGRect f = subview.frame; NSLog(@"sub %d -> x:%.0f, y:%.0f, w:%.0f, h:%.0f", i, f.origin.x, f.origin.y, f.size.width, f.size.height); if(f.origin.x == browserCanvas.origin.x && f.origin.y == browserCanvas.origin.y && f.size.width == browserCanvas.size.width && f.size.height == browserCanvas.size.height) { f.origin.y = header.frame.size.height; subview.frame = f; } } if(![header superview]) { [web.scrollView addSubview:header]; [web.scrollView bringSubviewToFront:header]; }
我认为最好的解决方案是将标题添加为 UIWebView 的 scrollView 的子视图,调整 UIEdgeInsets,并在 scrollViewDidScroll 委托函数中根据需要调整 scrollIndicatorInsets。
我发现的所有其他解决方案都没有考虑滚动指示器。
由于获得恰到好处的行为有点复杂,我决定编写一个类来处理所有这些:MMWebViewWithSmartHeader。它的边缘仍然有点粗糙,但应该有所帮助。
我特别喜欢 AutoLayout,因为我可以使标题的高度动态化。
这是我在控制器中使用 AutoLayout 的代码,其中对标题视图引用“headerView”,对 webView 引用“webView”。
// Function called in ViewDidLoad:
func addHeaderToWebView(){
// We load the headerView from a Nib
headerView = NSBundle.mainBundle().loadNibNamed(WebViewHeader.nibName, owner: self, options: nil).first as! WebViewHeader
headerView.translatesAutoresizingMaskIntoConstraints = false
webView.scrollView.addSubview(headerView)
// the constraints
let topConstraint = NSLayoutConstraint(item: headerView, attribute: .Bottom, relatedBy: .Equal, toItem: webView.scrollView, attribute: .Top, multiplier: 1, constant: 0)
let leftConstraint = NSLayoutConstraint(item: headerView, attribute: .Leading, relatedBy: .Equal, toItem: webView, attribute: .Leading, multiplier: 1, constant: 0)
let rightConstraint = NSLayoutConstraint(item: headerView, attribute: .Trailing, relatedBy: .Equal, toItem: webView, attribute: .Trailing, multiplier: 1, constant: 0)
// we add the constraints
webView.scrollView.addConstraints([topConstraint])
webView.addConstraints([leftConstraint, rightConstraint])
}
// Function called in viewWillLayoutSubviews: to update the scrollview of the webView content inset
func updateWebViewScrollViewContentInset(){
webView.scrollView.contentInset = UIEdgeInsetsMake(headerView.frame.height, 0, 0, 0)
}
文章可能没有使用 UIWebView 来显示主要文章文本。要完成您的任务,几乎可以肯定需要 UIScrollView。可能不需要调整其子视图的大小。查看 Apple 提供的 UIScrollView 代码示例。
Enormego 发布了一些示例代码来实现下拉刷新:
https://github.com/enormego/EGOTableViewPullRefresh
真的没那么复杂。您只需创建标题栏并将其UITableView
作为子视图添加到您的表格视图中即可获得 Safari/Articles 样式的标题栏。我正在我现在正在开发的应用程序上这样做:
GBSpendingMeter *meterView = [[[GBSpendingMeter alloc] initWithFrame:CGRectMake(0,0,320,44)] autorelease];
// The meterView will appear pinned to the top of the table, similar to a headerView
[self.tableView addSubview:meterView];
棘手的部分是观察表格上的滚动事件(仅供参考,UITableView 是 UIScrollView 的子类,因此所有相同的通知/回调都应该起作用),但我链接到的示例代码有一个很好的例子来说明它是如何工作的。
尝试这个:
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, -100, WIN_WIDTH, 100)];
redView.backgroundColor = [UIColor redColor];
self.view.scrollView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
[self.view.scrollView addSubview:redView];
NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.baidu.com"]];
[self.view loadRequest:request];
我不能肯定地说,但 Twitter 似乎用来UITableView
实现这一点。也许您可以尝试将您的视图放在表格视图的标题中,并将您的内容放在一个单元格中。
这是一个您可能会考虑查看的有趣示例 - How to make a Pull-To-Reload TableView just like Tweetie 2