WebView的定位 - Monotouch

时间:2013-01-03 03:57:46

标签: webview xamarin.ios orientation

我有一个我似乎无法旋转的UIWebView。到目前为止,我的应用程序中的所有内容都可以正常工作,除了方向(我希望支持所有方向)。

在我的下面的代码中,我正在使用WebViewDelegate,以便向用户提供他们想要打开从网站下载的文件的本地应用程序的选择,而不仅仅是将其交给Safari。) p>

在我的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的所有(包括肖像和风景)。

任何人都可以给予的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用自定义渲染器在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);
    }
}
}