

Dealing with the System UI on iOS in Xamarin.Forms
source link: https://msicc.net/dealing-with-the-system-ui-on-ios-in-xamarin-forms/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Dealing with the System UI on iOS in Xamarin.Forms
Having written a few applications with Xamarin.Forms
by now, there was always the one part where you have to go platform specific. Over time, this part got easier as the collection of Platform-specifics in the Xamarin.Forms package was growing and growing.
This post will show (my) most used implementations leveraging the comfort of Platform-specifics as well as some other gotchas I collected over time. At the end of this post, you will also find a link to a demo project on my Github.
Large page title
Let’s start on top (literally). With iOS 11, Apple introduced large title’s that go back to small once the user is scrolling the content.
To make your app use this feature, you need two perform two steps. The first step is to tell your NavigationPage
instance to prefer large titles. I often do this when creating my apps MainPage
in App.xaml.cs
:
public App() { InitializeComponent(); var navigationPage = new Xamarin.Forms.NavigationPage(new MainPage()) { BarBackgroundColor = Color.DarkGreen, BarTextColor = Color.White }; navigationPage.On<iOS>().SetPrefersLargeTitles(true); MainPage = navigationPage; }
This opens the door to show large titles on all pages that are managed by this NavigationPage
instance. Sometimes, however, you need to actively tell the page it should use the large title (mostly happened to me in my base page implementation – never was able to nail it down to a specific point. I just opted in to always explicitly handle it on every page. In the sample application for this post, you will find a switch to toggle and untoggle the large title on the app’s MainPage
:
On<iOS>().SetLargeTitleDisplay(_useLargeTitle ? LargeTitleDisplayMode.Always : LargeTitleDisplayMode.Never);
You can read more in the documentation.
StatusBar text color
Chances are high that we are customizing the BarBackgroundColor
and BarTextColor
properties. Of course, it makes absolutely sense that the StatusBar
text follows the BarTextColor.
Luckily, there is a Platform-specific for that as well:
if (this.Parent is Xamarin.Forms.NavigationPage navigationPage) { navigationPage.On<iOS>().SetStatusBarTextColorMode(_statusBarTextFollowNavBarTextColor ? StatusBarTextColorMode.MatchNavigationBarTextLuminosity : StatusBarTextColorMode.DoNotAdjust); }
The documentation ends here. However, I always need to add/change the Info.plist
file as well:
<key>UIViewControllerBasedStatusBarAppearance</key> <false/>
Only after adding this value the above-mentioned trick for the StatusBar
text works.
NavigationBar Separator
On iOS, the NavigationBar
has a separator on its bottom. If you want to hide this separator (which always disturbs the view), you can leverage another Platform-specific on your page:
if (this.Parent is Xamarin.Forms.NavigationPage navigationPage) { navigationPage.On<iOS>().SetHideNavigationBarSeparator(_hideNavBarSeparator); }
Home indicator visibility
All iPhones after the iPhone 8 (except the SE 2) do not have the home button. Instead, they have a home indicator on the bottom of the device (at least in app). If you are trying to set the color on it, I have bad news for you: you can’t (read on to learn why).
You can hide the indicator in your app, however. Just use this Platform-specific:
On<iOS>().SetPrefersHomeIndicatorAutoHidden(_hideHomeIndicator);
Home indicator background color
Hiding the home indicator is a hard measure. Most users do not even really recognize the indicator if it is incorporated into the app’s UI. To better understand how the home indicator works, I absolutely recommend to read Nathan Gitter’s great post on the topic.
The home indicator is adaptive to its surroundings. Most probably using a matching background color is all it needs to integrate the indicator nicely in your app(s).
Safe area
Thanks to the notch and the home indicator, putting content of our apps got trickier than before. However, Xamarin.Forms
has you covered as well. All we have to do is to use the SetUseSafeArea Platform-specific – it will allow us to just use the area where we are not covering any System UI like the StatusBar
or the home indicator:
On<iOS>().SetUseSafeArea(_useSafeArea);
Conclusion
Even though iOS has some specialties when it comes to the System UI, Xamarin.Forms
has the most important tools built in to deal with them. I absolutely recommend creating a base page for your applications and set the most common specifics there. You can find the promised demo project here on Github. Like always, I hope this post is helpful for some of you.
Until the next post, happy coding, everyone!
Like this:
Recommend
-
101
-
64
Change Randomly A View’s Color Hi everyone, here is the third part of the Xamarin Forms Tips series. Today, we will look at how to change a...
-
54
Hot off the press, another PR just got merged into the Xamarin.Forms repository. This time, adding a new feature on a Label, the ability to specify
-
34
Overview Paging is one of things you will need to implement especially when you are dealing with large amount of data from your server, this scenario applies to both web applications, winforms and mobile. Today...
-
56
For a pull request I opened on the Xamarin .Forms repository, I had imple...
-
42
A new feature was merged into the Xamarin.Forms repository. This time: TextColor for the TableSection control. With this new feature, you can specify the color of the text that is used in a table section. A small feature,...
-
17
Xamarin Forms – iOS floating tabbar Sometimes the design of our apps requires us to create custom elements inside of Xamarin Forms. One thing I wanted to try out, was using a floating tabbar instead of a bottom fixed one.
-
15
Color the iOS Status Bar in Xamarin.Forms307 views•Jan 2, 2021 After publishi...
-
8
Opening A PDF in Xamarin Forms Pages (Part1: Xamarin.iOS Custom Control)By DoumerMAUI , Xamarin , Xamarin Forms Tips and Tricks...
-
4
Use the iOS system colors in Xamarin.Forms (Updated) Apple has defined some best practices for coloring our iOS apps. To make it easier, Apple created system colors that are (partly) adaptive to the system's coloring. In...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK