{"id":20833,"date":"2016-02-15T09:45:09","date_gmt":"2016-02-15T17:45:09","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=20833"},"modified":"2016-02-15T09:45:09","modified_gmt":"2016-02-15T17:45:09","slug":"building-dynamic-layouts-for-ios-with-uistackview","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/building-dynamic-layouts-for-ios-with-uistackview\/","title":{"rendered":"Building Dynamic Layouts for iOS with UIStackView"},"content":{"rendered":"<p>\t\t\t\tLaying out views on iOS has traditionally been very difficult. Before the birth of the iPad and different iPhone form-factors, developers would often hardcode view positions, but this is no longer a viable approach as the device landscape has broadened. To solve this, Apple introduced AutoLayout, which can <a href=\"https:\/\/blog.xamarin.com\/autolayout-101-building-beautiful-dynamic-uis-for-all-devices\/\">dynamically adjust your user interface<\/a> depending on device form factor by using a constraints-based layout system. AutoLayout solved many issues, but is difficult to learn, fragile to changes, and can be extremely time consuming to get right. One of the more frustrating aspects of this approach is that if I wish to add a control between different parts of my content, I&#8217;ll need to break a number of existing constraints and rebuild the layout almost from scratch to ensure it works perfectly.<\/p>\n<p>What if we could layout views in a dynamic manner that was also <em>extremely<\/em> easy? One of my favorite additions to iOS 9 is a brand\u00a0new control named <code>UIStackView<\/code> to help streamline the process of laying out interface elements. This new control is unlike any\u00a0existing controls we&#8217;ve used before with iOS. In this blog post, I&#8217;m going to detail how you can get started building dynamic, beautiful UIs with <code>UIStackView<\/code> today.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/UIStackView-sample.png\"><img decoding=\"async\" class=\"aligncenter wp-image-20834 \" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/UIStackView-sample-1024x491.png\" alt=\"UIStackView sample\" width=\"673\" height=\"323\" \/><\/a><\/p>\n<h2>Introducing UIStackView<\/h2>\n<p>Apple has taken note of the pain of AutoLayout\u00a0and has provided a new tool that aims to make our lives easier. <code>UIStackView<\/code> eliminates the pain of inner constraints by handling the creation of the the constraints at runtime based on the simple properties you\u2019ve set. This means we can drag and drop new controls into a <code>UIStackView<\/code> and have the correct constraints automatically set for us. It even allows for adding and removing views at runtime (you&#8217;d set it to be hidden).<\/p>\n<p>Instead of working with constraints like you would with AutoLayout, you use self-descriptive properties such as Axis, Spacing, Alignment, and Distribution with <code>UIStackView<\/code>. These are accessible from the Properties Pad, which makes it as simple as a few mouse clicks to get your UI looking great on every iOS device.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/uistack_hero_2x.png\"><img decoding=\"async\" class=\"aligncenter wp-image-20835 \" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/uistack_hero_2x.png\" alt=\"uistack_hero_2x\" width=\"541\" height=\"381\" \/><\/a><\/p>\n<p><code>UIStackView<\/code> is an interesting addition to our toolbox, as it is a non-rendering subclass of <code>UIView<\/code>. Because it is non-rendering, you don&#8217;t have the ability to set its background color or override its Draw method. Instead, it&#8217;s just a control for helping you with the layouts of its subviews. Another key limitation of <code>UIStackView<\/code> is that it&#8217;s not compatible with devices running below iOS 9. This means that if you need to support backwards compatibility, you&#8217;ll need to continue using constraints (be sure to check your <a href=\"http:\/\/insights.xamarin.com\">Xamarin Insights<\/a> data to see how many users are using pre-iOS 9 devices).<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-02-15-at-16.23.19.png\"><img decoding=\"async\" class=\"aligncenter wp-image-24586 size-large\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-02-15-at-16.23.19-1024x721.png\" alt=\"Xamarin Studio UIStackViews\" width=\"1024\" height=\"721\" \/><\/a><\/p>\n<h2>Adding and Removing Views from StackLayout<\/h2>\n<p><code>UIStackView<\/code> has an API for adding and removing subviews, alleviating any worries about your layout. Any views you wish to view within the stack view will need to be added using the ArrangedSubviews\u00a0property rather than SubView property.<\/p>\n<pre class=\"lang:csharp decode:true\">\n\/\/Add View\nMyUIStackView.AddArrangedSubview(myView);\n\n\/\/Remove view\nMyUIStackView.RemoveArrangedSubview(myView);\n<\/pre>\n<p>Extending this a little further allows us to easily add an animate effect to the addition and removal of views.<\/p>\n<pre class=\"lang:csharp decode:true\">\/\/ Animate stack\nUIView.Animate(0.25, ()=&gt;{\n    \/\/ Adjust stack view\n    MyUIStackView.LayoutIfNeeded();\n});\n\n<\/pre>\n<h3>Learn More<\/h3>\n<p>We have fantastic documentation on how to get started with <a href=\"https:\/\/developer.xamarin.com\/guides\/ios\/platform_features\/introduction_to_ios9\/uistackview\/\">UIStackViews<\/a> over on our documentation site.<\/p>\n<p><code>UIStackView<\/code>s are available with the latest version of Xamarin.iOS for both Xamarin Studio and Visual Studio. If you&#8217;re looking to target iOS 9 as a minimum, you&#8217;ll find <code>UIStackView<\/code>s dramatically improve the rate at which you can build a user interface that looks great on all iOS form factors.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laying out views on iOS has traditionally been very difficult. Before the birth of the iPad and different iPhone form-factors, developers would often hardcode view positions, but this is no longer a viable approach as the device landscape has broadened. To solve this, Apple introduced AutoLayout, which can dynamically adjust your user interface depending on [&hellip;]<\/p>\n","protected":false},"author":1929,"featured_media":20835,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[6,4],"class_list":["post-20833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-ios","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Laying out views on iOS has traditionally been very difficult. Before the birth of the iPad and different iPhone form-factors, developers would often hardcode view positions, but this is no longer a viable approach as the device landscape has broadened. To solve this, Apple introduced AutoLayout, which can dynamically adjust your user interface depending on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/20833","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\/1929"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=20833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/20833\/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=20833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=20833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=20833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}