xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.63k stars 1.87k forks source link

[Bug] UINavigationBar BackgroundColor can no longer be changed #13291

Closed PaulMercer-SDS closed 2 years ago

PaulMercer-SDS commented 3 years ago

Description

NavigationBar background colour can no longer be changed with UINavigationBar.Appearance.BackgroundColor

Steps to Reproduce

In AppDelegate.FinishedLaunching, UINavigationBar.Appearance.BackgroundColor = UIColor.Blue;

Expected Behavior

Expect background colour of navigationbar to change

Actual Behavior

Background colour of NavigationBar remains white

Basic Information

Environment

Show/Hide Visual Studio info Microsoft Visual Studio Enterprise 2019 Version 16.8.3 VisualStudio.16.Release/16.8.3+30804.86 Microsoft .NET Framework Version 4.8.04084 Installed Version: Enterprise Architecture Diagrams and Analysis Tools 00435-60000-00000-AA113 Microsoft Architecture Diagrams and Analysis Tools Visual C++ 2019 00435-60000-00000-AA113 Microsoft Visual C++ 2019 ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools ASA Service Provider 1.0 ASP.NET and Web Tools 2019 16.8.554.20160 ASP.NET and Web Tools 2019 ASP.NET Core Razor Language Services 16.1.0.2052803+84e121f1403378489b842e1797df2f3f5a49ac3c Provides languages services for ASP.NET Core Razor. ASP.NET Web Frameworks and Tools 2019 16.8.554.20160 For additional information, visit https://www.asp.net/ Azure App Service Tools v3.0.0 16.8.554.20160 Azure App Service Tools v3.0.0 Azure Data Lake Node 1.0 This package contains the Data Lake integration nodes for Server Explorer. Azure Data Lake Tools for Visual Studio 2.6.1000.0 Microsoft Azure Data Lake Tools for Visual Studio Azure Functions and Web Jobs Tools 16.8.554.20160 Azure Functions and Web Jobs Tools Azure Stream Analytics Tools for Visual Studio 2.6.1000.0 Microsoft Azure Stream Analytics Tools for Visual Studio C# Tools 3.8.0-5.20604.10+9ed4b774d20940880de8df1ca8b07508aa01c8cd C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration. IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info JetBrains ReSharper Ultimate 2020.1.3 Build 201.0.20200515.122017 JetBrains ReSharper Ultimate package for Microsoft Visual Studio. For more information about ReSharper Ultimate, visit http://www.jetbrains.com/resharper. Copyright © 2021 JetBrains, Inc. Microsoft Azure HDInsight Azure Node 2.6.1000.0 HDInsight Node under Azure Node Microsoft Azure Hive Query Language Service 2.6.1000.0 Language service for Hive query Microsoft Azure Stream Analytics Language Service 2.6.1000.0 Language service for Azure Stream Analytics Microsoft Azure Stream Analytics Node 1.0 Azure Stream Analytics Node under Azure Node Microsoft Azure Tools 2.9 Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30924.1 Microsoft Continuous Delivery Tools for Visual Studio 0.4 Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE. Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Microsoft Library Manager 2.1.113+g422d40002e.RR Install client-side libraries easily to any web project Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers Microsoft Visual C++ Wizards 1.0 Microsoft Visual C++ Wizards Microsoft Visual Studio Tools for Containers 1.1 Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container. Microsoft Visual Studio VC Package 1.0 Microsoft Visual Studio VC Package Mono Debugging for Visual Studio 16.8.43 (00471f8) Support for debugging Mono processes with Visual Studio. NuGet Package Manager 5.8.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info Snapshot Debugging Extension 1.0 Snapshot Debugging Visual Studio Extension Detailed Info SQL Server Data Tools 16.0.62010.06180 Microsoft SQL Server Data Tools SQL Server Reporting Services 15.0.19124.0 Microsoft SQL Server Reporting Services Designers Version 15.0.19124.0 ToolWindowHostedEditor 1.0 Hosting json editor into a tool window TypeScript Tools 16.0.21016.2001 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 3.8.0-5.20604.10+9ed4b774d20940880de8df1ca8b07508aa01c8cd Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Visual F# Tools 16.8.0-beta.20507.4+da6be68280c89131cdba2045525b80890401defd Microsoft Visual F# Tools Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio Visual Studio Container Tools Extensions 1.0 View, manage, and diagnose containers within Visual Studio. Visual Studio Spell Check Everywhere VSSpellCheckEverywhere An extension that enables spell checking within any Visual Studio file editor or tool window that uses WPF text boxes. https://GitHub.com/EWSoftware/VSSpellChecker Visual Studio Spell Checker VSSpellChecker An editor extension that checks the spelling of comments, strings, and plain text as you type or interactively with tool windows. https://GitHub.com/EWSoftware/VSSpellChecker Visual Studio Tools for Containers 1.0 Visual Studio Tools for Containers VisualStudio.DeviceLog 1.0 Information about my package VisualStudio.Foo 1.0 Information about my package VisualStudio.Mac 1.0 Mac Extension for Visual Studio Web Compiler 1.12.394 Compiler for LESS, Sass and CoffeeScript files Xamarin 16.8.000.260 (d16-8@fd405a2) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 16.8.0.507 (remotes/origin/d16-8@e87b24884) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 16.8.112 (86385a3) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 11.1.0.17 (d16-8/c0e2b8e) Xamarin.Android Reference Assemblies and MSBuild support. Mono: be2226b Java.Interop: xamarin/java.interop/d16-8@79d9533 ProGuard: Guardsquare/proguard/proguard6.2.2@ebe9000 SQLite: xamarin/sqlite/3.32.1@1a3276b Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-8@2fb1cbc Xamarin.iOS and Xamarin.Mac SDK 14.6.0.15 (87a1b18d8) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Build Logs

Screenshots

Reproduction Link

Workaround

raz0r777 commented 3 years ago

I have the same problem. Any solution?

PaulMercer-SDS commented 3 years ago

Any update on this? It is holding back release of our enterprise app to a client who has strict corporate style and colour requirements.

pboisso commented 3 years ago

Same here, any fix coming in?

PaulMercer-SDS commented 3 years ago

Is this likely to ever be fixed???

lostptr commented 3 years ago

For anyone that's dealing with this issue, I found a workaround but its not perfect. On AppDelegate.cs:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    ...
    UIColor color = Color.FromHex("FBAF4E").ToUIColor();
    UINavigationBar.Appearance.BackgroundColor = color;
    UINavigationBar.Appearance.BarTintColor = color;

    return base.FinishedLaunching(app, options);
}

image

Coundn't find a way to fix the status bar though...

thisismyself commented 2 years ago

This bug is almost one year old, it happens in some of the first executed lines of many apps, but nothing has happend here? A fix here would be appreciated very much! This is one reason we still stick on XF 4.8 as long as possible.

jfversluis commented 2 years ago

Hey everyone, sorry this one slipped through somehow! Can anyone maybe provide a reproduction for this? I'm not really sure how you would style the navigation bar like this and why you wouldn't use the Forms API for this?

FWIW, I think this isn't a Forms issue or at least this is in the mix here as well; the way iOS changed their behavior here in iOS 13. See: https://sarunw.com/posts/uinavigationbar-changes-in-ios13/

But seeing that it still works for 4.8 it seems like there is still some Forms issue going on here... Anyway, a repro project would still be very helpful to understand how you all are trying to use this :)

PaulMercer-SDS commented 2 years ago

Hi jfversluis As an example, this is what we have been doing in the AppDelegate FinishedLaunching() method

            UINavigationBar.Appearance.TintColor = UIColor.White;
            UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White });
            UINavigationBar.Appearance.BackgroundColor = UIColor.Blue;
            UINavigationBar.Appearance.BarTintColor = UIColor.Blue;
jfversluis commented 2 years ago

@PaulMercer-SDS could you do me a favor and check if it still works on iOS 12? Else I will try. I have a feeling in what piece of code this is going wrong. We probably don't check if anyone is setting it this way and we then override it.

Also my questions stands, why do you set it like this? I think we have APIs do to all of this in Forms as well, right?

jfversluis commented 2 years ago

OK it does work on iOS 12 as expected provided that you don't set it (override it) through the Forms APIs. Then I have a pretty good idea where this is happening.

jfversluis commented 2 years ago

~Hey all, a PR (#14923) for this is open now, would you be able to grab the NuGet as described here and let us know if this fixes this issue? That will greatly speed up the review process.~

~Besides verifying if this particular issue is fixed also be sure to check other scenarios in the same area to make sure that this fix doesn't accidentally has side-effects 🙂~

~Thanks!~

Hm nope, this one seems to crash

jfversluis commented 2 years ago

@PaulMercer-SDS OK, I have a version I think that does the correct thing as per the comment above. If you would mind testing it? Or anyone else of course :)

jfversluis commented 2 years ago

Gentle reminder I would love some feedback on this so it can be included in the next release!

pboisso commented 2 years ago

Morning @jfversluis, I thought the proposed fix was crashing as per your edited comment, so we did not try. We just gave a try to build 7902 to our code base and still no luck. The Navigation bar is still not the color we wanted.

This is how we set it on our end. Working fine in XF4.8

                UINavigationBar.Appearance.BarTintColor = AccentColor;
                UINavigationBar.Appearance.TintColor = TextColor;
                UINavigationBar.Appearance.TitleTextAttributes = new UIStringAttributes
                {
                    ForegroundColor = TextColor,
                };
jfversluis commented 2 years ago

@pboisso I can see the confusion, sorry about that :)

Mind you that you need to test a very specific version as the PR builds will only have one specific change and not all the rest. In this case you should be testing version 7887. Would you mind trying that one? Thanks!

pboisso commented 2 years ago

Thanks for the clarification on the specific build number, @jfversluis.

Unfortunately, XF 7887 installed and still broken for our project. Both (iOS 14.4 & iOS 15) still show white navigation bar with build 7887 whereas they are the proper color with XF 4.8.1821.

I tested in Debug mode with the simulator on iPhone 12 mini with iOS 14.4 and iPhone 13 mini with iOS 15.0.

jfversluis commented 2 years ago

Interesting. However trivial it may sound, would you be able to put this in a reproduction sample? Just start a new Xamarin.Forms project and add the code that you expect to work, but doesn't. Then I can see exactly what you're trying to do. If you could create a GitHub repo for it that would be amazing.

Secondly, is there any specific reason you don't use the Xamarin.Forms APIs that can do this for you?

pboisso commented 2 years ago

@jfversluis , I guess we never used the Xamarin.Forms API because we never had to. :) Apps were originally built using UINavigationBar.Appearance for some reason (most likely because we originally built our app using Xamarin Classic and moved to Forms later). Would you have a code snippet or a link to a reference on how to do the same with XF Api by any luck?

Thanks!

jfversluis commented 2 years ago

That makes sense! There are different ways to do it, but the NavigationPage has properties for:

The only thing looking at your code, is that you can't set the tint color and the text color separately I think. I believe the tint color will also be set to BarTextColor.

You can set this directly to your NavigationPage instance, create a inheritance or do it through styles.

Just to be absolutely sure: could you try the nightly feed (instructions in the README on the frontpage) that should be version 7903 and confirm it still doesn't work? If so, if it's not too much trouble, would you be able to create a reproduction project that shows your issue?

That might seem trivial, but will help me understand your situation better and also gives me something to test code changes on. Thanks!

pboisso commented 2 years ago

Hi @jfversluis, we finally ended up implementing the style to the NavigationPage.BarBackgroundColor and BarTextColor like you suggested and it's working well now! We never saw build 7903 (sounds like it is stuck to 7902) so we could not test the fix for the native method just yet, sorry about that, but very happy that it's working now, we will finally migrate over XF5! Thanks so much for your help, it's good to see someone caring for developers out there. :) Have a great one.

jfversluis commented 2 years ago

Awesome @pboisso, thanks you for the kind words! Don't worry about the other thing if this is working for you :) Let me know if you need anything else!