From 74391e0769d0e0f4be015147ddfa39bf25c90928 Mon Sep 17 00:00:00 2001 From: "Ngo Iok Ui (Wu Yu Wei)" Date: Sat, 13 Aug 2022 13:30:54 +0800 Subject: [PATCH] fix(ios): addSubview instead of setContentView (#655) Co-authored-by: Yu-Wei Wu --- .changes/ios-subview.md | 5 +++++ src/webview/wkwebview/mod.rs | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 .changes/ios-subview.md diff --git a/.changes/ios-subview.md b/.changes/ios-subview.md new file mode 100644 index 000000000..d5037f69d --- /dev/null +++ b/.changes/ios-subview.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +On iOS, add webview as subview instead of replacing original view. diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index c85511751..b40918380 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -279,16 +279,24 @@ impl InnerWebView { // [preference setValue:@YES forKey:@"fullScreenEnabled"]; let _: id = msg_send![_preference, setValue:_yes forKey:NSString::new("fullScreenEnabled")]; - // Initialize webview with zero point - let zero = CGRect::new(&CGPoint::new(0., 0.), &CGSize::new(0., 0.)); - let _: () = msg_send![webview, initWithFrame:zero configuration:config]; - - // Auto-resize on macOS #[cfg(target_os = "macos")] { + // Initialize webview with zero point + let zero = CGRect::new(&CGPoint::new(0., 0.), &CGSize::new(0., 0.)); + let _: () = msg_send![webview, initWithFrame:zero configuration:config]; + // Auto-resize on macOS webview.setAutoresizingMask_(NSViewHeightSizable | NSViewWidthSizable); } + #[cfg(target_os = "ios")] + { + let ui_view = window.ui_view() as id; + let frame: CGRect = msg_send![ui_view, frame]; + // set all autoresizingmasks + let () = msg_send![webview, setAutoresizingMask: 31]; + let _: () = msg_send![webview, initWithFrame:frame configuration:config]; + } + // Message handler let ipc_handler_ptr = if let Some(ipc_handler) = attributes.ipc_handler { let cls = ClassDecl::new("WebViewDelegate", class!(NSObject)); @@ -520,8 +528,8 @@ r#"Object.defineProperty(window, 'ipc', { #[cfg(target_os = "ios")] { - let ui_window = window.ui_window() as id; - let _: () = msg_send![ui_window, setContentView: webview]; + let ui_view = window.ui_view() as id; + let _: () = msg_send![ui_view, addSubview: webview]; } Ok(w)