{"id":34078,"date":"2017-11-03T14:31:34","date_gmt":"2017-11-03T21:31:34","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=34078"},"modified":"2019-04-04T09:00:27","modified_gmt":"2019-04-04T16:00:27","slug":"making-ios-11-even-easier-xamarin-forms","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/making-ios-11-even-easier-xamarin-forms\/","title":{"rendered":"Making iOS 11 Even Easier with Xamarin.Forms"},"content":{"rendered":"<p>\t\t\t\tiOS 11 has introduced a few new visual design updates that you may want to take advantage of, including safe area layout guides and large titles. For Xamarin.iOS developers, we\u2019ve recently published an <a href=\"https:\/\/developer.xamarin.com\/guides\/ios\/platform_features\/introduction-to-ios11\/updating-your-app\/visual-design\/\">iOS 11 guide for updating your Xamarin.iOS apps<\/a> and articles for <a href=\"https:\/\/blog.xamarin.com\/updating-xamarin-ios-apps-for-the-iphone-x\/\">safe area<\/a> and <a href=\"https:\/\/blog.xamarin.com\/go-large-ios-11\/\">large titles<\/a>. Of course, since Xamarin.Forms is a layer running on top of Xamarin.iOS, all of these features are immediately available to you!<\/p>\n<p>This post will show you how we\u2019re going a step further to make using these features even easier for you directly in Xamarin.Forms.<\/p>\n<h2>Using SafeArea layout in iOS 11<\/h2>\n<p>To ensure your content appears in the visible area of the device screen, especially to avoid the &#8220;notch&#8221; when running landscape on an iPhone X, you\u2019ll want to take advantage of the safe area layout guides that iOS 11 defines. We <a href=\"https:\/\/blog.xamarin.com\/updating-xamarin-ios-apps-for-the-iphone-x\/\">recently wrote<\/a> about how this is used in Xamarin.iOS when creating auto layout constraints.<\/p>\n<p>As always, in Xamarin.Forms you can reach into the Xamarin.iOS platform code to get a reference to those values and update your views accordingly.<\/p>\n<p>But we want to make it <em>even easier<\/em> for you. In the 2.4.0 Service Release 4, we&#8217;re introducing a <a href=\"https:\/\/developer.xamarin.com\/guides\/xamarin-forms\/platform-features\/platform-specifics\/\">platform-specific<\/a> for pages to use the\u00a0safe area layout guides to create appropriate insets in your pages. To opt-in, add this to your ContentPage constructor:<\/p>\n<pre class=\"marking:true lang:c# mark:1,12 decode:true\">using Xamarin.Forms.PlatformConfiguration.iOSSpecific;\nusing Xamarin.Forms;\n\nnamespace iPhoneX \n{\n\tpublic partial class ItemsPage : ContentPage\n\t{\n\t\tpublic ItemsPage()\n\t\t{\n\t\t\tInitializeComponent();\n\n\t\t\tOn&lt;Xamarin.Forms.PlatformConfiguration.iOS&gt;().SetUseSafeArea(true);\n\t\t}\n\t}\n}<\/pre>\n<p>Or alternatively in XAML:<\/p>\n<pre class=\"marking:true lang:xhtml mark:4-5 decode:true \">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt; \n&lt;ContentPage xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\" \n    xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\" \n    xmlns:ios=\"clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core\" \n    ios:Page.UseSafeArea=\"true\" &gt;<\/pre>\n<p>On iOS 11 and newer, the safe area bounds defined by Apple will be used to set <code>Padding<\/code> for the content of your page layouts. Note, this will override <code>Padding<\/code> values you may have previously set. Enable this\u00a0platform-specific on every page where you\u00a0require it.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-34080\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screenshot-2017-11-03-08.12.23.png\" alt=\"\" width=\"1928\" height=\"1090\" \/><\/p>\n<p>Xamarin.Forms provides some sensible defaults for the inset values, but if you want finer control you should either set the padding yourself in the constructor or retrieve the <code>Thickness<\/code> values from <code>SafeAreaInsets()<\/code> and adjust the values to\u00a0set the padding on your views to meet your needs in the <code>OnAppearing<\/code> method.<\/p>\n<pre class=\"lang:c# decode:true\">protected override void OnAppearing()\n{\n\tbase.OnAppearing();\n\n\tvar safeInsets  = On&lt;Xamarin.Forms.PlatformConfiguration.iOS&gt;().SafeAreaInsets();\n\tsafeInsets.Left = 24;\n\tthis.Padding = safeInsets;\n}<\/pre>\n<h2>Large Titles<img decoding=\"async\" class=\"alignright wp-image-34081\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screenshot-2017-11-03-08.08.35.png\" alt=\"\" width=\"177\" height=\"351\" \/><\/h2>\n<p>Part of the new iOS 11 update is support for a\u00a0larger display of your titles that appear in the navigation bar. As\u00a0with safe areas, you can now use a platform-specific to opt-in to this visual design pattern. When enabled, the title that usually appears in the center of the navigation bar will appear left aligned and, of course, larger. In landscape the title will return to the center to optimize the\u00a0view visible for content.<\/p>\n<p>The first step is to set your title preference either on the <code>NavigationPage<\/code> or the <code>ContentPage<\/code>.<\/p>\n<pre class=\"lang:c# decode:true \">On&lt;Xamarin.Forms.PlatformConfiguration.iOS&gt;().SetPrefersLargeTitles(true);<\/pre>\n<p>Now all pages within that navigation\u00a0page will display large titles. To control this at the <code>ContentPage<\/code> level, you may set the preference there. In this snippet, the preference is set to never use large titles:<\/p>\n<pre class=\"marking:true lang:c# mark:6 decode:true \">public partial class ItemsPage : ContentPage\n{\n    public ItemsPage()\n    {\n        InitializeComponent();\n        On&lt;Xamarin.Forms.PlatformConfiguration.iOS&gt;().SetLargeTitleDisplay(LargeTitleDisplayMode.Never);\n    }\n    ...\n}<\/pre>\n<p>The <code>SetLargeTitleDisplay<\/code> method takes three options:<\/p>\n<ul>\n<li>Always<\/li>\n<li>Automatic<\/li>\n<li>Never<\/li>\n<\/ul>\n<p>Always and Never are clear enough. Automatic is the default that means the behavior is inherited from\u00a0the previous page in the navigation stack.<\/p>\n<p><strong>Web Preview Available Today<\/strong>\nThese platform-specifics can be used today via a web preview. Visit our <a href=\"https:\/\/github.com\/xamarin\/\/releases\/tag\/release-2.4.0-sr4\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub releases page<\/a> to download the NuGet packages. We are actively testing these new platform specifics and taking feedback. If no changes are necessary, we\u2019ll promote this build as it is to NuGet for general availability.<\/p>\n<p><a href=\"https:\/\/forums.xamarin.com\/106097\/making-ios-11-even-easier-with-xamarin-forms\/p1?new=1\"><em>Discuss this post in the forums<\/em><\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>iOS 11 has introduced a few new visual design updates that you may want to take advantage of, including safe area layout guides and large titles. For Xamarin.iOS developers, we\u2019ve recently published an iOS 11 guide for updating your Xamarin.iOS apps and articles for safe area and large titles. Of course, since Xamarin.Forms is a [&hellip;]<\/p>\n","protected":false},"author":553,"featured_media":34081,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[16],"class_list":["post-34078","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>iOS 11 has introduced a few new visual design updates that you may want to take advantage of, including safe area layout guides and large titles. For Xamarin.iOS developers, we\u2019ve recently published an iOS 11 guide for updating your Xamarin.iOS apps and articles for safe area and large titles. Of course, since Xamarin.Forms is a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/34078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/553"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=34078"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/34078\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=34078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=34078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=34078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}