Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translucent and Transparent NavigationBar on iOS #19204

Merged
merged 27 commits into from Jan 24, 2024
Merged

Conversation

tj-devel709
Copy link
Contributor

@tj-devel709 tj-devel709 commented Dec 4, 2023

Description of Change

On MAUI iOS, there are some issues with trying to create transparent and translucent UINavigationBars that were possible in Xamarin.Forms. With the changes in this PR, it is now possible to get the super cool styling of a transparent and translucent navbar on NavigationPage, Shell, and FlyoutPage. Because iOS 15 made some changes to the UINavigationBar, there were some differences in the code depending in the iOS version and I will provide screenshots from iOS 11.0 and iOS 17.0.

For the following examples, please note that IgnoreSafeArea="True"is set on the top-level grid in the contentpage.

NavigationPage

When enabling transparency and translucent for the UINavigationBar in a NavigationPage, you'll want to make the NavigationPage's BarBackgroundColor = Colors.Transparent as well as calling yourNavigationPage.On<iOS>().EnableTranslucentNavigationBar() and yourNavigationPage.On<iOS>().SetHideNavigationBarSeparator(true)

NavigationPage Images and Code Sample (click me!)

iOS 17.0 iOS 11.0

MainPage.xaml:

    <ContentPage
        xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="Maui.Controls.Sample.MainPage"
        xmlns:local="clr-namespace:Maui.Controls.Sample"
        xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
        Title="Home"
        >
        <Grid IgnoreSafeArea="True" Background="#333333" RowDefinitions="300,*">
            <BoxView Grid.Row="0" Background="yellow" />
            <Image Source="dotnet_bot.png" />
            <Label Text="Hello, world!" Grid.Row="1"
                    TextColor="White"
                    HorizontalOptions="Center"
                    VerticalOptions="Center" />
        </Grid>
    </ContentPage>

MauiProgram.cs

    class App : Microsoft.Maui.Controls.Application
        {
            protected override Window CreateWindow(IActivationState activationState)
            {
                var navPage = new Microsoft.Maui.Controls.NavigationPage(new MainPage())
                {
                    BarBackgroundColor = Colors.Transparent,
                };
                navPage.On<iOS>().EnableTranslucentNavigationBar();
                navPage.On<iOS>().SetHideNavigationBarSeparator(true);
                return new Window(navPage);
            }
        }

FlyoutPage

Similar to the NavigationPage example above, you'll want to make the NavigationPage's BarBackgroundColor = Colors.Transparent as well as calling yourNavigationPage.On<iOS>().EnableTranslucentNavigationBar() and yourNavigationPage.On<iOS>().SetHideNavigationBarSeparator(true). You will then set this navigationPage to the 'Detail' property of the FlyoutPage.

FlyoutPage Images and Code Sample (click me!)

iOS 17.0 iOS 11.0

MainPage.xaml:

    <ContentPage
        xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="Maui.Controls.Sample.MainPage"
        xmlns:local="clr-namespace:Maui.Controls.Sample"
        xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
        Title="Home"
        >
        <Grid IgnoreSafeArea="True" Background="#333333" RowDefinitions="300,*">
            <BoxView Grid.Row="0" Background="yellow" />
            <Image Source="dotnet_bot.png" />
            <Label Text="Hello, world!" Grid.Row="1"
                    TextColor="White"
                    HorizontalOptions="Center"
                    VerticalOptions="Center" />
        </Grid>
    </ContentPage>

SandboxFlyout.cs

using System;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration;
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;
using Microsoft.Maui.Graphics;
namespace Maui.Controls.Sample
{
    public partial class SandboxFlyout : Microsoft.Maui.Controls.FlyoutPage
    {
        public SandboxFlyout()
        {
            InitializeComponent();
            var mainPage = new MainPage();
            var navigationPage = new Microsoft.Maui.Controls.NavigationPage(mainPage);
            navigationPage.BarBackgroundColor = Colors.Transparent;
            navigationPage.On<iOS>().SetHideNavigationBarSeparator(true);
            navigationPage.On<iOS>().EnableTranslucentNavigationBar();
            Flyout = new FlyPage();
            Detail = navigationPage;
        }
    }
}

Shell

Shell is a little different than the NavigationPage and Flyout Page. Because there is not an existing public property to set for Translucence, if you set Shell.NavBarIsVisible="True" and Shell.BackgroundColor="Transparent", the Shell's navigationBar will now become transparent and translucent.

Shell Images and Code Sample (click me!)

iOS 17.0 iOS 11.0

MainPage.xaml:

<ContentPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="Maui.Controls.Sample.MainPage"
    xmlns:local="clr-namespace:Maui.Controls.Sample"
    xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
    Title="Home"
    Shell.NavBarIsVisible="True"
    Shell.BackgroundColor="Transparent"
    >
    <Grid IgnoreSafeArea="True" Background="#333333" RowDefinitions="300,*">
        <BoxView Grid.Row="0" Background="yellow" />
        <Image Source="dotnet_bot.png" />
        <Label Text="Hello, world!" Grid.Row="1"
                TextColor="White"
                HorizontalOptions="Center"
                VerticalOptions="Center" />
    </Grid>
</ContentPage>

Testing

I added a UITest that adds a transparent and translucent UINavigationBar with the NavigationPage inside the Appium UITests below:

UITest
NewSafeAreaNavBariOS.mov

Issues Fixed

Fixes #17022

@tj-devel709 tj-devel709 added area/navigation 🧭 NavigationPage platform/iOS 🍎 area/shell 🐢 Shell Navigation, Routes, Tabs, Flyout labels Dec 4, 2023
@tj-devel709 tj-devel709 requested a review from a team as a code owner December 4, 2023 23:37
@tj-devel709 tj-devel709 marked this pull request as draft December 4, 2023 23:37
@tj-devel709 tj-devel709 added this to the .NET 8 SR2 milestone Dec 4, 2023
@tj-devel709 tj-devel709 added the partner/cat 😻 Client CAT Team label Dec 5, 2023
@AlleSchonWeg
Copy link

Hi @tj-devel709,
there are some problems with translucent and safe area : #9053
Will this problems also be adressed?

@tj-devel709
Copy link
Contributor Author

Hi @tj-devel709, there are some problems with translucent and safe area : #9053 Will this problems also be adressed?

Hello @AlleSchonWeg! I would say that this PR probably does not affect the Large Titles transitions in the issue above but rather being able to have have visible content behind the UINavigationBar.

@tj-devel709 tj-devel709 marked this pull request as ready for review December 5, 2023 08:48
@AlleSchonWeg
Copy link

Okay,
this pull request reminded me of the linked issue. The combination of safe area, translucent, large tiles and LV/CV results in scrolling and snapping issues.

@davidortinau
Copy link
Contributor

Before and after. Looking like what I expect!

Simulator Screenshot - iPhone 15 Pro - 2023-12-13 at 15 02 18
Simulator Screenshot - iPhone 15 Pro - 2023-12-13 at 14 56 21

@PureWeen
Copy link
Member

/rebase

@samhouts samhouts added the stale Indicates a stale issue/pr and will be closed soon label Jan 8, 2024
@PureWeen PureWeen removed the stale Indicates a stale issue/pr and will be closed soon label Jan 22, 2024
Copy link
Contributor

Choose a reason for hiding this comment

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

Have the snapshots been generated on the server or locally? Must be generated on the CI server.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I generated the screenshots locally and moved them from the snapshot-diff to the snapshot directory. Are they different from the CI?

@PureWeen PureWeen merged commit cb1ac24 into main Jan 24, 2024
47 checks passed
@PureWeen PureWeen deleted the dev/TJ/SafeAreaNav branch January 24, 2024 21:21
rmarinho added a commit that referenced this pull request Jan 29, 2024
* Add Obsolete tag for old IndexOf (#20068)

* Split the InputTransparency tests (#19925)

* Bump Xamarin.UITest to 4.3.4 (#20067)

* Bump Xamarin.UITest to 4.3.4

* Update NUnit

* Restore dotnet tool (#20106)

* Do not use underscores in the ApplicationId (#19377)

* Do not use underscores in the ApplicationId

Underscores are not supported on Windows

* Update DotnetInternal.cs

* [WinUI] Add workaround for Connectivity check on Win10  (#19261)

* Implemented a Win10 work-around for connectivity thread issue

Reimplement `ConnectivityImplementation.ConnectionProfiles` to use native .net core APIs

* Remove prop bag ID

* Replace network availability changed event with native .net API

* Remove explicit file include

* Fix file naming

* Implemented a Win10 work-around for connectivity thread issue

Reimplement `ConnectivityImplementation.ConnectionProfiles` to use native .net core APIs

* Remove prop bag ID

* Replace network availability changed event with native .net API

* Remove explicit file include

* Fix file naming

* * Revert change to remove `Windows.Networking.Connectivity.NetworkInformation.NetworkStatusChanged`

* Update Connectivity.uwp.cs

---------

Co-authored-by: Mike Corsaro <mikecorsaro@microsoft.com>
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>

* [Windows] Adjust recycle check in ItemContentControl (#20079)

* Adjust recycle check in `ItemContentControl` to use Content property

* Fix confusing line

* Rectify the scopes in the builder (#19932)

* Update System.Drawing.Common (#20122)

* Enable building WASDK Self-Contained packaged apps (#20019)

* Translucent and Transparent NavigationBar on iOS (#19204)

* Changes for enabling translucent navigation bar ios

* Add UITest for NavigationPage Safe Area Translucence

* remove UIKit

* Move UITest from gallery to Issues

* push a new page for UITests, consolidate code, and save shadowimage

* Changes for enabling translucent navigation bar ios

* Add UITest for NavigationPage Safe Area Translucence

* remove UIKit

* Move UITest from gallery to Issues

* make the extension method

* use the background color alpha for translucent

* use same alpha logic for pre ios 15

* accidently added testing comments

* add more UITests for the different NavigationPage and Flyout Page scenarios

* use additionalsafeareainsets for the secondary toolbar

* missing new line

* only run the secondary toolbar offset when we have a secondary toolbar

* use existing childViewControllers

* remove the shadow stuff and simplify the expression with null

* style and fixes from merge conflicts

* standardAppearance and scrolledgeappearance should be kept in

* changes after talking with Shane

* change shell behavior to be more like navrenderer with preiOS13

* move code if we are transparent pre13 shell

* remove new lines

* add screenshot tests

* be able to remove and add secondarybar additionalsafeareas

* reset the xaml on the Sandboc MainPage (#20082)

* [Android] Fix gif animation initial state (#14295)

* Fix gifs initial animation state on Android

* Device tests

* Auto-format source code

* Updated sample

* Updated device tests

* Refactor code

* Update src/Core/src/Handlers/Image/ImageHandler.Android.cs

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>

* Remove unnecessary change

* Fix build errors

* Merge branch 'main' into fix-7019

* Fix merge mistake

---------

Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
Co-authored-by: Javier Suárez <6755973+jsuarezruiz@users.noreply.github.com>
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>

* Enable Windows Image device tests (#20167)

* [X] allow x:Type extension for BPConverter (#18540)

- fixes #18324

* Source Generator Performance Improvements (#19990)

* Source Generator Performance Improvements:
- Reversed lookup order (Extension second)
- Introduced type cache

```
PERF PROGRESS:
SourceGen - Maui.Controls.Sample (262 XAML files)

1) Unchanged:
- 15640 GetTypeByMetadata calls
- 223s

2) Extensions lookup in XmlTypeXamlExtensions.GetTypeReference() second
- 6232 GetTypeByMetadata calls (~60% reduction)
- 90s                          (~60% reduction)

3) With type cache
- 203 GetTypeByMetadata calls (~97% reduction => ~99% total reduction)
- 6s                          (~93% reduction => ~97% total reduction => 37 times faster!)
```

* - Set uinitial lookupNames capacity to 2 since there won't be more than 2

* Added appium UITest for FlyoutNavigationBetweenItemsWithNavigationStacks (#19444) Fixes #16675

* Add better exception for missing Maps on Windows (#19046)

* Add better exception for missing Maps on Windows

* Update AppHostBuilderExtensions.cs

* [ci] Add missing demands (#20183)

* Add comments to internal methods for XAML Hot Reload usage (#20215)

* Add comments to internal methods for XAML Hot Reload usage

* spacing

* Adding Mobile tag to MAUI project templates (#20191)

Co-authored-by: Luke Westendorf <lukewest@microsoft.com>

---------

Co-authored-by: Tim Miller <drasticactions@users.noreply.github.com>
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
Co-authored-by: Mike Corsaro <corsarom@gmail.com>
Co-authored-by: Mike Corsaro <mikecorsaro@microsoft.com>
Co-authored-by: TJ Lambert <50846373+tj-devel709@users.noreply.github.com>
Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>
Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
Co-authored-by: Javier Suárez <6755973+jsuarezruiz@users.noreply.github.com>
Co-authored-by: Stephane Delcroix <stephane@delcroix.org>
Co-authored-by: Marco Goertz <mgoertz@microsoft.com>
Co-authored-by: MSLukeWest <42553283+MSLukeWest@users.noreply.github.com>
Co-authored-by: Luke Westendorf <lukewest@microsoft.com>
@Cybrosys
Copy link

Cybrosys commented Feb 16, 2024

Is it possible to achieve the same on Android with Shell, the layout doesn't extend in under the NavigationBar during my tests?

@github-actions github-actions bot locked and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/navigation 🧭 NavigationPage area/shell 🐢 Shell Navigation, Routes, Tabs, Flyout partner/cat 😻 Client CAT Team platform/iOS 🍎
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can only ignore iOS Safe Area when navigation bar is hidden
7 participants