問題描述
有誰知道在 iOS 上避免滾動視圖中的網頁視圖的技術原因(假設您愿意自己禁用網頁視圖中的滾動)?
Does anyone know of technical reasons for avoiding web views inside scroll views on iOS (assuming you're willing to disable scrolling inside the web views themselves)?
如果您查看 Apple 文檔對于 UIWebView,他們聲明:
If you look at the Apple docs for UIWebView, they state:
重要提示:您不應將 UIWebView 或 UITableView 對象嵌入UIScrollView 對象.如果這樣做,可能會導致意外行為因為兩個對象的觸摸事件可能會混淆和錯誤處理.
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
我的猜測
看起來他們可能是在警告您不要將滾動視圖放在另一個滾動視圖中,因為內部滾動視圖和外部滾動視圖之間的觸摸可能會混淆.
My Educated Guess
It looks like maybe they're warning you against putting a scroll view inside another scroll view, because touches can be confused between the inner, and outer scroll views.
但是,將 UIWebView
放在滾動視圖中是有充分理由的.Web 視圖不僅僅是 滾動視圖.UIWebView
可以輕松顯示范圍廣泛的web 內容.
But, there's a very valid reason to want to put a UIWebView
inside a scroll view. Web views aren't just scroll views. UIWebView
can easily display a wide range of web content.
如果不需要允許 在 UIWebView
本身內滾動,并且您使用以下任一方法關閉滾動:
If there is no need to allow scrolling within the UIWebView
itself, and you turn off scrolling with either:
webView.userInteractionEnabled = NO;
或
webView.scrollView.scrollEnabled = NO;
那么這個設計真的有問題嗎?
then is there really any problem with this design?
我想知道這是否部分是原始 UIWebView
界面的工件,它沒有為您提供 直接(并記錄)訪問其嵌入的 UIScrollView
(能夠輕松禁用其滾動).也許 Apple 文檔中的這句話是遺留下來的?
I'm wondering if this is partly an artifact of the original UIWebView
interface, where it did not give you direct (and documented) access to its embedded UIScrollView
(to be able to disable its scrolling easily). Maybe this statement in the Apple docs is a legacy of that?
我問是因為我正在維護一個應用程序(由其他人編寫),該應用程序在允許水平滾動的滾動視圖中使用少數 Web 視圖.Web 內容必須被認為是固定的(不可更改),并且每個 HTML 頁面只顯示一頁內容.用戶需要能夠在頁面之間滾動,因此為此選擇了 UIScrollView
內的多個 UIWebViews
.到目前為止,它似乎可能工作正常.
I ask because I'm maintaining an app (written by someone else) that uses a handful of web views inside a scroll view that allows scrolling between them horizontally. The web content must be considered fixed (not changeable), and it only shows one page of content per HTML page. The user needs to be able to scroll between pages, so multiple UIWebViews
inside a UIScrollView
were chosen for that. So far, it appears that it may be working properly.
但是,頁面顯示全屏圖像,并且滾動性能是一個問題.但是,我正在嘗試確定滾動視圖中 Web 視圖的基本嵌套(Apple 警告不要這樣做)是否真的是問題的一部分.
However, the pages show full screen images, and scrolling performance is an issue. But, I'm trying to determine if the fundamental nesting of web views inside scroll views (which Apple warns against) is really part of the problem.
推薦答案
Apple 不建議將 UIWebViews
放在 UIScrollViews
中的唯一原因,如果你解釋的原因:因為滾動可能會在兩個滾動視圖之間混淆.
The only reason Apple does not recommend putting UIWebViews
inside UIScrollViews
if for the one you explain: because scrolling would risk to be mixed up between the two scroll views.
我猜他們寫這個是因為 UIWebView
繼承 UIView
而不是 UIScrollView
,因此它本身不是滾動視圖(但嵌入一個),這對于沒有經驗的用戶來說可能并不明顯,Web 內容可以根據 HTML 滾動,如果有的話,這會弄亂容器滾動視圖.所以這可能只是對這個案例的提醒.
I guess they wrote this because, by the fact that UIWebView
inherits UIView
and not UIScrollView
, and thus is not a scrollview itself (but embeds one), this may be not obvious for the unexperimented user that the web content can be scrollable depending on the HTML, which would mess up with the container scrollview if any. So that is probably just a reminder for this case.
但是如果你禁用滾動,我看不出有什么原因會出錯.
But if you disable the scrolling, I can't see any reason why this would go wrong.
請注意,在滾動視圖上禁用用戶交互與禁用滾動不同.如果您的 HTML 內容包含鏈接或其他可點擊/可點擊的內容,禁用用戶交互也會禁用它們.要僅禁用滾動但保持用戶交互(如簡單的點擊),請使用 webview.scrollView.scrollEnabled = NO
而不是 webview.scrollView.userInteractionEnabled = NO
.
Note anyway that disabling user interaction on the scrollview is not the same as disabling scrolling. If your HTML content contains links or other clickable/tappable content, disabling user interaction will disable them too. To only disable scrolling only but keep user interaction like simple taps, use webview.scrollView.scrollEnabled = NO
instead of webview.scrollView.userInteractionEnabled = NO
.
這篇關于為什么 UIWebView 不應該放在 UIScrollView 中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!