WebView 的方向 - Monotouch
Posted
技术标签:
【中文标题】WebView 的方向 - Monotouch【英文标题】:Orientation of WebView - Monotouch 【发布时间】:2013-01-03 03:57:46 【问题描述】:我有一个似乎无法旋转的 UIWebView。到目前为止,我的应用程序中的所有内容似乎都可以正常工作,但方向除外(我希望支持所有方向)。
在下面的代码中,我使用 WebViewDelegate 来向用户展示他们希望使用哪个本地应用程序来打开从网站下载的文件,而不是将其交给 Safari)。
在我的 UIViewController 中设置了(据我所知)
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
return true;
这是我的 AppDelegate。
Register ("AppDelegate")] public partial class AppDelegate : UIApplicationDelegate // class-level declarations UIWindow window; UIWebView webView;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
// create a new window instance based on the screen size
window = new UIWindow (UIScreen.MainScreen.ApplicationFrame);
webView = new UIWebView (window.Bounds);
window.AddSubview (webView);
string url = "https://www.myURL.com";
webView.LoadRequest(new NSUrlRequest (new NSUrl (url)));
webView.WeakDelegate = new WebViewDelegate();
webView.ScalesPageToFit = true;
var keys = new object[] "UserAgent" ;
var objects = new object[] "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (Khtml, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3" ;
var dictionnary = NSDictionary.FromObjectsAndKeys(objects, keys);
NSUserDefaults.StandardUserDefaults.RegisterDefaults(dictionnary);
// make the window visible
window.MakeKeyAndVisible ();
return true;
public class WebViewDelegate: UIWebViewDelegate
UIDocumentInteractionController docControl;
public WebViewDelegate () : base()
public override bool ShouldStartLoad (UIWebView webView, MonoTouch.Foundation.NSUrlRequest request, UIWebViewNavigationType navigationType)
var docString = request.Url.ToString ();
Uri uri = new Uri (docString);
string ext = Path.GetExtension (uri.ToString ()).ToLower();
var canShow = false;
switch (ext)
case ".xls":
case ".xlxs":
case ".doc":
case ".docx":
case ".pdf":
case ".jpeg":
case ".jpg":
case ".gif":
case ".png":
case ".mov":
case ".mp4":
case ".ogv":
case ".webm":
case ".avi":
case ".mpeg":
case ".mp3":
case ".wmv":
canShow = true;
break;
if (canShow)
WebClient _webClient = new WebClient();
_webClient.Headers.Add("SecretKey","SecretKey");
string filename = Path.GetFileName(uri.ToString());
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string filePath = Path.Combine(path, filename);
_webClient.DownloadFileAsync(uri,filePath);
_webClient.DownloadFileCompleted += (sender, e) =>
var docData = File.ReadAllBytes(filePath);
File.WriteAllBytes(filePath, docData);
InvokeOnMainThread(delegate
docControl = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(filePath));
docControl.Delegate = new UIDocumentInteractionControllerDelegate();
docControl.PresentOptionsMenu(new Rectangle(0,-260,320,320),webView,true);
);
;
//open it in safari...
//UIApplication.SharedApplication.OpenUrl(request.Url);
return false;
return true;
当然,在我的 Info.plist 文件中,我确实支持 iPhone 和 iPad 的所有内容(肖像和风景)。
任何人都可以提供任何帮助将不胜感激。
【问题讨论】:
您的 UIWebView 不是 UIViewController 的视图,而是直接附加到 UIWindow。我建议先阅读 UIViewController/MVCV 基础知识。 【参考方案1】:在here 找到了使用自定义渲染器的解决方案,但在 ios 7 中不起作用。
[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
namespace iOS
public class MyWebViewRenderer: WebViewRenderer
protected override void OnElementChanged(VisualElementChangedEventArgs e)
base.OnElementChanged(e);
this.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
this.AutosizesSubviews = true;
public override void LayoutSubviews()
base.LayoutSubviews();
var screenBound = UIKit.UIScreen.MainScreen.Bounds;
var offSetH = GetStatusBarHeight();
NativeView.Bounds = new CoreGraphics.CGRect(
screenBound.X,
screenBound.Y + offSetH,
screenBound.Width,
screenBound.Height - offSetH);
【讨论】:
以上是关于WebView 的方向 - Monotouch的主要内容,如果未能解决你的问题,请参考以下文章
iOS 8 上的 Facebook 登录 WebView 方向错误