Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

[iOS] Avoid setting embedding view controller background color #14761

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions EmbeddingTestBeds/Embedding.XF/TransparentPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Embedding.XF.TransparentPage"
BackgroundColor="Transparent">
<ContentPage.Content>
<StackLayout Margin="80">
<BoxView HeightRequest="100" BackgroundColor="#77ff0000" />
<Label Text="This page has a transparent background. The containing view background should be yellow and should be visible" />
<BoxView HeightRequest="100" BackgroundColor="#770000ff" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
14 changes: 14 additions & 0 deletions EmbeddingTestBeds/Embedding.XF/TransparentPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Embedding.XF
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class TransparentPage : ContentPage
{
public TransparentPage()
{
InitializeComponent();
}
}
}
11 changes: 11 additions & 0 deletions EmbeddingTestBeds/Embedding.iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class AppDelegate : UIApplicationDelegate
UIViewController _hello;
UIViewController _alertsAndActionSheets;
UIViewController _webview;
UIViewController _containingController;

UIBarButtonItem _helloButton;

Expand Down Expand Up @@ -91,5 +92,15 @@ public void ShowWebView()

_navigation.PushViewController(_webview, true);
}

public void ShowTransparent()
{
if (_containingController == null)
{
_containingController = new ContainingViewController();
}

_navigation.PushViewController(_containingController, true);
}
}
}
21 changes: 21 additions & 0 deletions EmbeddingTestBeds/Embedding.iOS/ContainingViewController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using UIKit;
using Embedding.XF;
using Xamarin.Forms.Platform.iOS;

namespace Embedding.iOS
{
public class ContainingViewController : UIViewController
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
View.BackgroundColor = UIColor.Yellow;

var tansparentController = new TransparentPage().CreateViewController();
AddChildViewController(tansparentController);
tansparentController.View.Frame = View.Bounds;
View.AddSubview(tansparentController.View);
tansparentController.DidMoveToParentViewController(this);
}
}
}
1 change: 1 addition & 0 deletions EmbeddingTestBeds/Embedding.iOS/Embedding.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="ViewController.designer.cs">
<DependentUpon>ViewController.cs</DependentUpon>
</Compile>
<Compile Include="ContainingViewController.cs" />
</ItemGroup>
<ItemGroup>
<InterfaceDefinition Include="Resources\LaunchScreen.xib" />
Expand Down
38 changes: 26 additions & 12 deletions EmbeddingTestBeds/Embedding.iOS/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,44 +1,58 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="198">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="198">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="197">
<objects>
<viewController id="198" sceneMemberID="viewController" customClass="ViewController">
<viewController id="198" customClass="ViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="195"/>
<viewControllerLayoutGuide type="bottom" id="196"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="199">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="209" translatesAutoresizingMaskIntoConstraints="NO" fixedFrame="YES">
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="209">
<rect key="frame" x="113" y="167" width="205" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Show Web View">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" id="210" translatesAutoresizingMaskIntoConstraints="NO" fixedFrame="YES">
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="210">
<rect key="frame" x="96" y="257" width="239" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Show Alerts And ActionSheets">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="ZYx-Eg-NIM">
<rect key="frame" x="96" y="347" width="239" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Show Transparent">
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
</button>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
<connections>
<outlet property="UIViewController" destination="199" id="name-outlet-199"/>
<outlet property="ShowAlertsAndActionSheets" destination="210" id="name-outlet-210"/>
<outlet property="ShowTransparent" destination="ZYx-Eg-NIM" id="6k8-bj-ams"/>
<outlet property="ShowWebView" destination="209" id="name-outlet-209"/>
<outlet property="UIViewController" destination="199" id="name-outlet-199"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="200" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="70" y="38"/>
</scene>
</scenes>
</document>
</document>
1 change: 1 addition & 0 deletions EmbeddingTestBeds/Embedding.iOS/ViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public override void ViewDidLoad()
base.ViewDidLoad();
ShowWebView.TouchUpInside += (sender, e) => AppDelegate.Shared.ShowWebView();
ShowAlertsAndActionSheets.TouchUpInside += (sender, e) => AppDelegate.Shared.ShowAlertsAndActionSheets();
ShowTransparent.TouchUpInside += (sender, e) => AppDelegate.Shared.ShowTransparent();
}
}
}
9 changes: 9 additions & 0 deletions EmbeddingTestBeds/Embedding.iOS/ViewController.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Xamarin.Forms.Platform.iOS/PlatformRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ public override void ViewWillAppear(bool animated)
// while it's being replaced on the Window.RootViewController with a new MainPage
if (!_disposed)
{
View.BackgroundColor = ColorExtensions.BackgroundColor;
Platform.WillAppear();
}

base.ViewWillAppear(animated);

if (!_disposed)
Copy link
Contributor Author

@GalaxiaGuy GalaxiaGuy Oct 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Platform.WillAppear sets the view's background color to ColorExtensions.BackgroundColor so the background color has to be set to Clear after that.

Is Platform.WillAppear only used for embedding? If so it may be better to make the change there.

{
View.BackgroundColor = UIColor.Clear;
}
}

public override void ViewDidLoad()
Expand Down