{"id":226,"date":"2015-04-28T14:34:23","date_gmt":"2015-04-28T14:34:23","guid":{"rendered":"http:\/\/ootips.org\/yonat\/?p=226"},"modified":"2016-04-17T06:56:22","modified_gmt":"2016-04-17T06:56:22","slug":"succinct-auto-layout","status":"publish","type":"post","link":"https:\/\/ootips.org\/yonat\/succinct-auto-layout\/","title":{"rendered":"Succinct Auto Layout"},"content":{"rendered":"<p>Adding constraints programmatically is a verbose endeavor.<\/p>\n<p>Several libraries try to solve this by adding an additional layer between your code and Auto Layout. (<a href=\"https:\/\/github.com\/smileyborg\/PureLayout\" target=\"_blank\">PureLayout<\/a>,<a href=\"https:\/\/github.com\/SnapKit\/Masonry\" target=\"_blank\">Masonry<\/a>, <a href=\"https:\/\/github.com\/SnapKit\/SnapKit\" target=\"_blank\">SnapKit<\/a>, <a href=\"https:\/\/github.com\/robotmedia\/Lyt\" target=\"_blank\">Lyt<\/a>, <a href=\"https:\/\/github.com\/robb\/Cartography\" target=\"_blank\">Cartography<\/a>, and others.)<br \/>\nWhile they make some things easier, they still require you to learn a new system with new quirks.<\/p>\n<p>Instead, I use <a href=\"https:\/\/gist.github.com\/yonat\/f5835c5ebda7e14ee203\" target=\"_blank\">MiniLayout<\/a> &#8212; one short file that simply takes the verbosity out of AutoLayout. It does this by using default values for most of NSLayoutConstraint&#8217;s parameters, and by compressing the cumbersome <code>view.addConstraint( NSLayoutConstraint(...) )<\/code> into a single call.<\/p>\n<p>Examples:<\/p>\n<p><strong>Put <code>label<\/code> over <code>textField<\/code><\/strong><\/p>\n<pre class=\"lang:swift decode:true \">\r\n\/\/ using MiniLayout:\r\nview.constrain(label, at: .Leading, to: textField)\r\nview.constrain(textField, at: .Top, to: label, at: .Bottom, diff: 8)\r\n\r\n\/\/ without MiniLayout:\r\nview.addConstraint( NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: Equal, toItem: textField, attribute: .Leading, multiplier: 1, constant: 0) )\r\nview.addConstraint( NSLayoutConstraint(item: textField, attribute: .Top, relatedBy: Equal, toItem: label, attribute: .Bottom, multiplier: 1, constant: 8) )\r\n<\/pre>\n<p><strong>Add <code>button<\/code> at the center of <code>view<\/code><\/strong><\/p>\n<pre class=\"lang:swift decode:true \">\r\n\/\/ using MiniLayout:\r\nview.addConstrainedSubview(button, constrain: .CenterX, .CenterY)\r\n\r\n\/\/ without MiniLayout:\r\nview.addSubview(button)\r\nbutton.setTranslatesAutoresizingMaskIntoConstraints(false)\r\nview.addConstraint( NSLayoutConstraint(item: button, attribute: .CenterX, relatedBy: Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0) )\r\nview.addConstraint( NSLayoutConstraint(item: button, attribute: .CenterY, relatedBy: Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0) )\r\n<\/pre>\n<p><a href=\"https:\/\/github.com\/yonat\/MiniLayout\" target=\"_blank\">MiniLayout<\/a> uses the same enums and the same logic as AutoLayout, there&#8217;s nothing new to learn. It just makes the code shorter and more readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding constraints programmatically is a verbose endeavor. Several libraries try to solve this by adding an additional layer between your code and Auto Layout. (PureLayout,Masonry, SnapKit, Lyt, Cartography, and others.) While they make some things easier, they still require you &hellip; <a href=\"https:\/\/ootips.org\/yonat\/succinct-auto-layout\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[7,6,17],"class_list":["post-226","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ios","tag-iphone","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2aEUP-3E","_links":{"self":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/comments?post=226"}],"version-history":[{"count":6,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"predecessor-version":[{"id":236,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/posts\/226\/revisions\/236"}],"wp:attachment":[{"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ootips.org\/yonat\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}