我在 Xamarin.Forms 项目中为 WebView 创建了一个自定义渲染器。WebView 在 Android 上运行良好。不幸的是,在 iOS 上,当设备旋转时,网页(任何网页)都无法正确调整到 WebView 的新尺寸。如果我使用内置的 WebView for Forms,页面会在旋转时正确调整大小。
我的自定义渲染器做错了什么?
下面是我的自定义渲染器的精简版本(问题仍然存在):
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using UIKit;
using Foundation;
[assembly: ExportRenderer(typeof(WebView), typeof(MyProject.iOS.WebViewRenderer))]
namespace MyProject.iOS {
public class WebViewRenderer : ViewRenderer<WebView, UIWebView> {
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e) {
if (Control == null) {
var webView = new UIWebView(Frame);
webView.AutoresizingMask = UIViewAutoresizing.All;
SetNativeControl(webView);
webView.LoadRequest(new NSUrlRequest(new NSUrl("http://cnn.com")));
}
}
}
}