Skip to content

Commit

Permalink
fix(ios): addSubview instead of setContentView (#655)
Browse files Browse the repository at this point in the history
Co-authored-by: Yu-Wei Wu <wusyong9104@gmail.com>
  • Loading branch information
wusyong and Yu-Wei Wu committed Aug 13, 2022
1 parent b8027ba commit 74391e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changes/ios-subview.md
@@ -0,0 +1,5 @@
---
"wry": patch
---

On iOS, add webview as subview instead of replacing original view.
22 changes: 15 additions & 7 deletions src/webview/wkwebview/mod.rs
Expand Up @@ -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));
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 74391e0

Please sign in to comment.