1

我有一个似乎无法旋转的 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 的所有内容(包括肖像和风景)。

任何人都可以提供的任何帮助将不胜感激。

4

1 回答 1

0

通过使用自定义渲染器在此处找到解决方案,但在 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);
    }
}
}
于 2015-08-12T15:16:35.997 回答