Skip to content

Commit 14e8287

Browse files
committed
feat(ios): add support for setting the background color
1 parent 2c1c874 commit 14e8287

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ objc2-ui-kit = { version = "0.3.0", default-features = false, features = [
169169
"UIWindow",
170170
"UIApplication",
171171
"UIEvent",
172+
"UIColor"
172173
] }
173174

174175
[target.'cfg(target_os = "macos")'.dependencies]

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ pub struct WebViewAttributes<'a> {
478478
///
479479
/// ## Platform-specific:
480480
///
481-
/// - **macOS / iOS**: Not implemented.
481+
/// - **macOS**: Not implemented.
482482
/// - **Windows**:
483483
/// - On Windows 7, transparency is not supported and the alpha value will be ignored.
484484
/// - On Windows higher than 7: translucent colors are not supported so any alpha value other than `0` will be replaced by `255`
@@ -844,7 +844,7 @@ impl<'a> WebViewBuilder<'a> {
844844
///
845845
/// ## Platfrom-specific:
846846
///
847-
/// - **macOS / iOS**: Not implemented.
847+
/// - **macOS**: Not implemented.
848848
/// - **Windows**:
849849
/// - on Windows 7, transparency is not supported and the alpha value will be ignored.
850850
/// - on Windows higher than 7: translucent colors are not supported so any alpha value other than `0` will be replaced by `255`
@@ -1972,7 +1972,7 @@ impl WebView {
19721972
///
19731973
/// ## Platfrom-specific:
19741974
///
1975-
/// - **macOS / iOS**: Not implemented.
1975+
/// - **macOS**: Not implemented.
19761976
/// - **Windows**:
19771977
/// - On Windows 7, transparency is not supported and the alpha value will be ignored.
19781978
/// - On Windows higher than 7: translucent colors are not supported so any alpha value other than `0` will be replaced by `255`

src/wkwebview/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,24 @@ impl InnerWebView {
370370
let frame = ns_view.frame();
371371
let webview: Retained<WryWebView> =
372372
objc2::msg_send![super(webview), initWithFrame: frame, configuration: &**config];
373+
if let Some((red, green, blue, alpha)) = attributes.background_color {
374+
// This is required first since the webview color is applied too late.
375+
webview.setOpaque(false);
376+
377+
let color = objc2_ui_kit::UIColor::colorWithRed_green_blue_alpha(
378+
(red / 255).into(),
379+
(green / 255).into(),
380+
(blue / 255).into(),
381+
(alpha / 255).into(),
382+
);
383+
384+
if !is_child {
385+
ns_view.setBackgroundColor(Some(&color));
386+
}
387+
// This has to be monitored as it may clash with isOpaque = true.
388+
// The webview background color may also applied too late so actually not that useful.
389+
webview.setBackgroundColor(Some(&color));
390+
}
373391
webview
374392
};
375393

@@ -831,6 +849,25 @@ r#"Object.defineProperty(window, 'ipc', {
831849
}
832850

833851
pub fn set_background_color(&self, _background_color: RGBA) -> Result<()> {
852+
#[cfg(target_os = "ios")]
853+
unsafe {
854+
let (red, green, blue, alpha) = _background_color;
855+
856+
let color = objc2_ui_kit::UIColor::colorWithRed_green_blue_alpha(
857+
(red / 255).into(),
858+
(green / 255).into(),
859+
(blue / 255).into(),
860+
(alpha / 255).into(),
861+
);
862+
863+
if !self.is_child {
864+
self.ns_view.setBackgroundColor(Some(&color));
865+
}
866+
// This has to be monitored as it may clash with isOpaque = true.
867+
// The webview background color may also applied too late so actually not that useful.
868+
self.webview.setBackgroundColor(Some(&color));
869+
}
870+
834871
Ok(())
835872
}
836873

0 commit comments

Comments
 (0)