Hello,
You can do it by handler for webview. I add webview handler in the page's background code. Then you can use Conditional compilation for iOS platform achievement.
You can refer to the following code. Note:You can add iOS implementation blew the android implementation.
Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
{
if (view is CustomWebView)
{
CustomWebView customWebView = view as CustomWebView;
#if ANDROID
handler.PlatformView.SetWebViewClient(new XamWebViewClient(customWebView));
#elif IOS
try
{
handler.PlatformView.NavigationDelegate = new ExtendedWKWebViewDelegate(customWebView);
if (handler.PlatformView != null)
{
var webView = (WKWebView)handler.PlatformView;
webView.Opaque = false;
webView.BackgroundColor = UIColor.Clear;
webView.ScrollView.ScrollEnabled = false;
}
}
catch (Exception ex)
{
Console.WriteLine("Error at WebViewHandler: " + ex.Message);
}
#endif
}
});
#if IOS
/// <summary>
/// ExtendedWKWebViewDelegate
/// </summary>
class ExtendedWKWebViewDelegate : WKNavigationDelegate
{
private CustomWebView customWebView;
public ExtendedWKWebViewDelegate(CustomWebView customWebView)
{
this.customWebView = customWebView;
}
/// <summary>
/// DidFinishNavigation
/// </summary>
/// <param name="webView"></param>
/// <param name="navigation"></param>
public override async void DidFinishNavigation(WKWebView webView, WKNavigation navigation)
{
if (customWebView != null && webView != null)
{
await System.Threading.Tasks.Task.Delay(100); // wait here till content is rendered
if (webView.ScrollView != null && webView.ScrollView.ContentSize != null)
{
customWebView.HeightRequest = (double)webView.ScrollView.ContentSize.Height;
}
}
}
}
#endif
Best Regards, Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.