{"id":106,"date":"2025-10-04T08:53:19","date_gmt":"2025-10-03T23:53:19","guid":{"rendered":"https:\/\/devserver.kr\/blog\/?p=106"},"modified":"2025-10-04T08:53:19","modified_gmt":"2025-10-03T23:53:19","slug":"ios-autolayout","status":"publish","type":"post","link":"https:\/\/devserver.kr\/blog\/ios\/ios-autolayout\/","title":{"rendered":"iOS Autolayout"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><h3>autolayout programmatically<\/h3>\n<pre><code class=\"language-swift\">translatesAutoresizingMaskIntoConstraints = false\n<\/code><\/pre>\n<pre><code class=\"language-swift\">let constraints = [\n    view.centerXAnchor.constraint(equalTo: superview.centerXAnchor),\n    view.centerYAnchor.constraint(equalTo: superview.centerYAnchor),\n    view.widthAnchor.constraint(equalToConstant: 100),\n    view.heightAnchor.constraint(equalTo: view.widthAnchor)\n]\nNSLayoutConstraint.activate(constraints)\n\n\nNSLayoutConstraint.activate([\n    view.centerXAnchor.constraint(equalTo: superview.centerXAnchor),\n    view.centerYAnchor.constraint(equalTo: superview.centerYAnchor),\n    view.widthAnchor.constraint(equalToConstant: 100),\n    view.heightAnchor.constraint(equalTo: view.widthAnchor)\n])\n<\/code><\/pre>\n<h2>view \uc5d0 \uac00\ub2a5\ud55c anchor \ubcc0\uc218<\/h2>\n<pre><code class=\"language-swift\">var bottomAnchor: NSLayoutYAxisAnchor\n\/\/A layout anchor representing the bottom edge of the view\u2019s frame.\nvar centerXAnchor: NSLayoutXAxisAnchor\n\/\/A layout anchor representing the horizontal center of the view\u2019s frame.\nvar centerYAnchor: NSLayoutYAxisAnchor\n\/\/A layout anchor representing the vertical center of the view\u2019s frame.\nvar firstBaselineAnchor: NSLayoutYAxisAnchor\n\/\/A layout anchor representing the baseline for the topmost line of text in the view.\nvar heightAnchor: NSLayoutDimension\n\/\/A layout anchor representing the height of the view\u2019s frame.\nvar lastBaselineAnchor: NSLayoutYAxisAnchor\n\/\/A layout anchor representing the baseline for the bottommost line of text in the view.\nvar leadingAnchor: NSLayoutXAxisAnchor\n\/\/A layout anchor representing the leading edge of the view\u2019s frame.\nvar leftAnchor: NSLayoutXAxisAnchor\n\/\/A layout anchor representing the left edge of the view\u2019s frame.\nvar rightAnchor: NSLayoutXAxisAnchor\n\/\/A layout anchor representing the right edge of the view\u2019s frame.\nvar topAnchor: NSLayoutYAxisAnchor\n\/\/A layout anchor representing the top edge of the view\u2019s frame.\nvar trailingAnchor: NSLayoutXAxisAnchor\n\/\/A layout anchor representing the trailing edge of the view\u2019s frame.\nvar widthAnchor: NSLayoutDimension\n\/\/A layout anchor representing the width of the view\u2019s frame.\n<\/code><\/pre>\n<pre><code class=\"language-swift\">Building Constraints\nfunc constraint(equalTo: NSLayoutAnchor&lt;AnchorType&gt;) -&gt; NSLayoutConstraint\n\/\/Returns a constraint that defines one item\u2019s attribute as equal to another.\nfunc constraint(equalTo: NSLayoutAnchor&lt;AnchorType&gt;, constant: CGFloat) -&gt; NSLayoutConstraint\n\/\/Returns a constraint that defines one item\u2019s attribute as equal to another item\u2019s attribute plus a constant offset.\nfunc constraint(greaterThanOrEqualTo: NSLayoutAnchor&lt;AnchorType&gt;) -&gt; NSLayoutConstraint\n\/\/Returns a constraint that defines one item\u2019s attribute as greater than or equal to another.\nfunc constraint(greaterThanOrEqualTo: NSLayoutAnchor&lt;AnchorType&gt;, constant: CGFloat) -&gt; NSLayoutConstraint\n\/\/Returns a constraint that defines one item\u2019s attribute as greater than or equal to another item\u2019s attribute plus a constant offset.\nfunc constraint(lessThanOrEqualTo: NSLayoutAnchor&lt;AnchorType&gt;) -&gt; NSLayoutConstraint\n\/\/Returns a constraint that defines one item\u2019s attribute as less than or equal to another.\nfunc constraint(lessThanOrEqualTo: NSLayoutAnchor&lt;AnchorType&gt;, constant: CGFloat) -&gt; NSLayoutConstraint\n\/\/Returns a constraint that defines one item\u2019s attribute as less than or equal to another item\u2019s attribute plus a constant offset.\n<\/code><\/pre>\n<h2>layout guide<\/h2>\n<pre><code>layoutMarginsGuide: Set constraints and keep the layout margins as spacing\nreadableContentGuide: Constraints the width to a size that is easy for the user to read\nsafeAreaLayoutGuide: Represents the portion of your view that is unobscured by bars and other content\n<\/code><\/pre>\n<pre><code class=\"language-swift\">greenView.heightAnchor.constraint(equalTo: greenView.widthAnchor, multiplier: 16\/9).isActive = true\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,7],"tags":[],"class_list":["post-106","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts\/106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/comments?post=106"}],"version-history":[{"count":1,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts\/106\/revisions"}],"predecessor-version":[{"id":107,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts\/106\/revisions\/107"}],"wp:attachment":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/media?parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/categories?post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/tags?post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}