<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UILayoutGuide",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(cs)UILayoutGuide"
  },
  "title" : "UILayoutGuide"
}
-->

# UILayoutGuide

A rectangular area that can interact with Auto Layout.

```
@MainActor class UILayoutGuide
```

## Overview

Use layout guides to replace the placeholder views you may have created to represent inter-view spaces or encapsulation in your user interface. Traditionally, there were a number of Auto Layout techniques that required placeholder views. A placeholder view is an empty view that does not have any visual elements of its own and serves only to define a rectangular region in the view hierarchy. For example, if you wanted to use constraints to define the size or location of an empty space between views, you needed to use a placeholder view to represent that space. If you wanted to center a group of objects, you needed a placeholder view to contain those objects. Similarly, placeholder views could be used to contain and encapsulate part of your user interface. Placeholder views let you break up a large, complex user interface into self-contained, modular chunks. When used properly, they could greatly simplify your Auto Layout constraint logic.

There are a number of costs associated with adding placeholder views to your view hierarchy. First, there is the cost of creating and maintaining the view itself. Second, the placeholder view is a full member of the view hierarchy, which means that it adds overhead to every task the hierarchy performs. Worst of all, the invisible placeholder view can intercept messages that are intended for other views, causing problems that are very difficult to find.

The [`UILayoutGuide`](/documentation/UIKit/UILayoutGuide) class is designed to perform all the tasks previously performed by placeholder views, but to do it in a safer, more efficient manner. Layout guides do not define a new view. They do not participate in the view hierarchy. Instead, they simply define a rectangular region in their owning view’s coordinate system that can interact with Auto Layout.

### Creating Layout Guides

To create a layout guide, you must perform the following steps:

1. Instantiate a new layout guide.
2. Add the layout guide to a view by calling the view’s [`addLayoutGuide(_:)`](/documentation/UIKit/UIView/addLayoutGuide(_:)) method.
3. Define the position and size of the layout guide using Auto Layout.

You can use these guides to define the space between elements in your layout. The following example shows layout guides used to define an equal spacing between a series of views.

```objc
UILayoutGuide *space1 = [[UILayoutGuide alloc] init];
[self.view addLayoutGuide:space1];
 
UILayoutGuide *space2 = [[UILayoutGuide alloc] init];
[self.view addLayoutGuide:space2];
 
[space1.widthAnchor constraintEqualToAnchor:space2.widthAnchor].active = YES;
[self.saveButton.trailingAnchor constraintEqualToAnchor:space1.leadingAnchor].active = YES;
[self.cancelButton.leadingAnchor constraintEqualToAnchor:space1.trailingAnchor].active = YES;
[self.cancelButton.trailingAnchor constraintEqualToAnchor:space2.leadingAnchor].active = YES;
[self.clearButton.leadingAnchor constraintEqualToAnchor:space2.trailingAnchor].active = YES;
```

A layout guide can also act as an opaque box that contains other views and controls, letting you encapsulate parts of your view and break up your layout into modular chunks.

```objc
UILayoutGuide *container = [[UILayoutGuide alloc] init];
[self.view addLayoutGuide:container];
 
// Layout the contents of the container
[self.label.lastBaselineAnchor constraintEqualToAnchor:self.textField.lastBaselineAnchor].active = YES;
[self.label.leadingAnchor constraintEqualToAnchor:container.leadingAnchor].active = YES;
[self.textField.leadingAnchor constraintEqualToAnchor:self.label.trailingAnchor constant:8.0].active = YES;
[self.textField.trailingAnchor constraintEqualToAnchor:container.trailingAnchor].active = YES;
[self.textField.topAnchor constraintEqualToAnchor:container.topAnchor].active = YES;
[self.textField.bottomAnchor constraintEqualToAnchor:container.bottomAnchor].active = YES;
 
// Set exterior constraints.
UILayoutGuide *margins = self.view.layoutMarginsGuide;
 
[container.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor].active = YES;
[container.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor].active = YES;
[container.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor constant:20.0].active = YES;
```

> Note:
> Layout guides provides a lightweight method for encapsulating part of your layout. Note that this technique only affects how Auto Layout interacts with the encapsulated views. It does not change the view hierarchy in any way. However, this is not the only way to create modular user interfaces. Container views and container view controllers provide an even greater degree of encapsulation, letting you separate the layout, the view hierarchy and even the related view controller code. For more information, see [Adaptivity and Size Changes](https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/TheAdaptiveModel.html#//apple_ref/doc/uid/TP40007457-CH18) in [View Controller Programming Guide for iOS](https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/index.html#//apple_ref/doc/uid/TP40007457).
> 
> Additionally, layout constraints do not fully encapsulate their contents. The system still compares the priority of optional constraints inside the layout guide with the priority of optional constraints outside the guide.

## Topics

### Working with layout guides

[`identifier`](/documentation/UIKit/UILayoutGuide/identifier)

A string used to identify the layout guide.

[`layoutFrame`](/documentation/UIKit/UILayoutGuide/layoutFrame)

The layout guide’s frame in its owning view’s coordinate system.

[`owningView`](/documentation/UIKit/UILayoutGuide/owningView)

The view that owns this layout guide.

### Creating constraints using layout anchors

[`bottomAnchor`](/documentation/UIKit/UILayoutGuide/bottomAnchor)

A layout anchor representing the bottom edge of the layout guide’s frame.

[`centerXAnchor`](/documentation/UIKit/UILayoutGuide/centerXAnchor)

A layout anchor representing the horizontal center of the layout guide’s frame.

[`centerYAnchor`](/documentation/UIKit/UILayoutGuide/centerYAnchor)

A layout anchor representing the vertical center of the layout guide’s frame.

[`heightAnchor`](/documentation/UIKit/UILayoutGuide/heightAnchor)

A layout anchor representing the height of the layout guide’s frame.

[`leadingAnchor`](/documentation/UIKit/UILayoutGuide/leadingAnchor)

A layout anchor representing the leading edge of the layout guide’s frame.

[`leftAnchor`](/documentation/UIKit/UILayoutGuide/leftAnchor)

A layout anchor representing the left edge of the layout guide’s frame.

[`rightAnchor`](/documentation/UIKit/UILayoutGuide/rightAnchor)

A layout anchor representing the right edge of the layout guide’s frame.

[`topAnchor`](/documentation/UIKit/UILayoutGuide/topAnchor)

A layout anchor representing the top edge of the layout guide’s frame.

[`trailingAnchor`](/documentation/UIKit/UILayoutGuide/trailingAnchor)

A layout anchor representing the trailing edge of the layout guide’s frame.

[`widthAnchor`](/documentation/UIKit/UILayoutGuide/widthAnchor)

A layout anchor representing the width of the layout guide’s frame.

### Debugging the layout guide

[`-  constraintsAffectingLayoutForAxis:`](/documentation/UIKit/UILayoutGuide/constraintsAffectingLayout(for:))

The constraints that impact the layout of the guide.

[`hasAmbiguousLayout`](/documentation/UIKit/UILayoutGuide/hasAmbiguousLayout)

A Boolean value indicating whether the constraints impacting the layout guide specify its location ambiguously.

### Handling keyboard layout

[`keyboardLayoutGuide`](/documentation/UIKit/UIView/keyboardLayoutGuide)

A layout guide that tracks the keyboard’s position in your app’s layout.

[`UIKeyboardLayoutGuide`](/documentation/UIKit/UIKeyboardLayoutGuide)

A layout guide that represents the space the keyboard occupies in your app’s layout.

[`UITrackingLayoutGuide`](/documentation/UIKit/UITrackingLayoutGuide)

A layout guide that automatically activates and deactivates layout constraints depending on its proximity to edges.

## Relationships

### Conforms To

[`Copyable`](/documentation/Swift/Copyable)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Escapable`](/documentation/Swift/Escapable)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Sendable`](/documentation/Swift/Sendable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Hashable`](/documentation/Swift/Hashable)

[`Equatable`](/documentation/Swift/Equatable)

[`UIPopoverPresentationControllerSourceItem`](/documentation/UIKit/UIPopoverPresentationControllerSourceItem)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

### Inherited By

[`UIFocusGuide`](/documentation/UIKit/UIFocusGuide)

[`UITrackingLayoutGuide`](/documentation/UIKit/UITrackingLayoutGuide)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)