問題描述
我想向 UIWebView
添加一個標(biāo)題視圖,類似于 MobileSafari 中的地址/搜索欄和 Sophia Teutschler 的優(yōu)秀 Articles.app.更準(zhǔn)確地說,我想在 UIWebView 上方創(chuàng)建一個拉動固定方向"視圖,就像在文章中一樣.文章確實使用了 UIWebView,所以這似乎是可能的.如 這篇文章?顯然,我確實需要滾動事件以使拉動以固定方向"行為相應(yīng).
I'd like to add a header view to an UIWebView
similar to the address/search bar in MobileSafari and the excellent Articles.app by Sophia Teutschler. More precisely, I'd like to create a "pull to fix orientation" view above a UIWebView, just like in Articles. Articles does use a UIWebView, so it seems to be possible. Is there a way to accomplish this without having to embed the UIWebView
into a UIScrollView
and updating its size all the time, as described in this article? Apparently, I do need the scrolling events to have the "pull to fix orientation" behave accordingly.
推薦答案
經(jīng)過幾次嘗試,我決定采用這個解決方案:
After several attemps, I've decided to go with this solution:
基本上我剛剛將我的標(biāo)題 UIView 添加為 webview.scrollview 子視圖.
Basically I've just added my header UIView as a webview.scrollview subview.
為避免標(biāo)題與瀏覽器視圖重疊:
To avoid having the header overlap the browser view:
- 循環(huán)瀏覽所有 [webview.scrollView 子視圖]
- 尋找最大的,應(yīng)該包含瀏覽器(其他用于陰影和線條)
改變它的起源.y
- cycle through all [webview.scrollView subviews]
- look for the biggest, wich should contains the browser (the others are for shadows and lines)
change its origin.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];
}
這篇關(guān)于將標(biāo)題視圖添加到類似于 Safari 和文章的 UIWebView的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!