### Basic PinLayout Usage Example (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Demonstrates the fundamental usage of PinLayout for setting up view layouts. This is often the starting point for integrating PinLayout into your project. ```swift // Example of basic PinLayout usage in Swift // Assuming 'view' is an instance of UIView or a subclass view.pin.top().left().width(100).height(50) ``` -------------------------------- ### PinLayout Usage Examples Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Examples demonstrating how to use PinLayout for common layout tasks. ```APIDOC ## Usage Examples ### Horizontally Between Views ```swift view.pin.horizontallyBetween(viewA, and: viewB, aligned: .top) ``` ### Vertically Between Views ```swift view.pin.verticallyBetween(viewC, and: viewD, aligned: .center) ``` ### Vertically Between Views with Margin This example positions a view between two other views vertically, and centers it relative to the first view with a top and bottom margin of 10 pixels. ```swift view.pin.verticallyBetween(viewA, and: viewB, aligned: .center).marginVertical(10) ``` ``` -------------------------------- ### Objective-C: Basic Layout Example Source: https://github.com/layoutbox/pinlayout/blob/master/docs/objective_c.md This example demonstrates the basic layout of views using PinLayout in Objective-C, similar to the Swift Intro example. It utilizes `pinObjc` for layout properties and requires an explicit `layout()` call to apply changes. ```Objective-C - (void) layoutSubviews { [super layoutSubviews]; CGFloat margin = 10; UIEdgeInsets safeArea = self.pinObjc.safeArea; logo.pinObjc.topInsets(safeArea).leftInsets(safeArea).width(100).aspectRatio().margin(margin).layout(); segmented.pinObjc.rightOfAligned(logo, VerticalAlignTop).rightInsets(safeArea).marginHorizontal(margin).layout(); textLabel.pinObjc.belowOfAligned(segmented, HorizontalAlignLeft).widthOf(segmented).pinEdges().marginTop(margin).sizeToFitType(FitWidth).layout(); separatorView.pinObjc.belowOfViewsAligned(@[logo, textLabel], HorizontalAlignLeft).rightToEdge(segmented.edge.right).height(1).marginTop(margin).layout(); } ``` -------------------------------- ### RTL Layout Example in Swift Source: https://github.com/layoutbox/pinlayout/blob/master/docs/rtl_support.md Demonstrates how to use PinLayout's RTL-aware methods (start, end, after, below) within the layoutSubviews function. This example shows positioning elements like logos, segmented controls, text labels, and separators while respecting RTL layout principles. ```swift override func layoutSubviews() { super.layoutSubviews() // Layout the contentView using the view's safeArea contentView.pin.all(pin.safeArea) let padding: CGFloat = 10 logo.pin.top().start(padding).width(100).aspectRatio().marginTop(10) segmented.pin.after(of: logo, aligned: .top).end(padding).marginStart(10) textLabel.pin.below(of: segmented, aligned: .start).width(of: segmented).pinEdges().marginTop(10).sizeToFit(.width) separatorView.pin.below(of: [logo, textLabel], aligned: .start).end(to: segmented.edge.end).marginTop(10) } ``` -------------------------------- ### PinLayout Usage Examples Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Illustrative examples demonstrating various PinLayout configurations for view positioning and sizing. Includes examples for single edge pinning, multiple edge pinning, centering, and respecting safe area insets. ```swift view.pin.top(20).bottom(20) view.pin.top().left() view.pin.all() view.pin.all(pin.safeArea) view.pin.top(25%).hCenter() view.pin.left(12).vCenter() view.pin.start(20).end(20) view.pin.horizontally(20) view.pin.top().horizontally() ``` -------------------------------- ### PinLayout Simple Form Example (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Provides a basic example of using PinLayout to lay out elements within a form, demonstrating how to arrange labels, input fields, and buttons efficiently. ```swift // Example form layout using PinLayout label.pin.top(20).horizontally(10).height(30) textField.pin.below(of: label).horizontally(10).height(40) ``` -------------------------------- ### PinLayout CALayer Support Example (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Shows how PinLayout can be used to layout CALayer's. Highlights limitations such as `sizeToFit` and safe area properties not being directly available. ```swift // Example of PinLayout usage on CALayer myCALayer.pin.frame(CGRect(x: 10, y: 10, width: 50, height: 50)) ``` -------------------------------- ### PinLayout Objective-C Example Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Demonstrates the usage of PinLayout within an Objective-C codebase. This allows Objective-C developers to leverage PinLayout's declarative layout system. ```objc // Example of PinLayout usage in Objective-C // Assuming 'aView' is an instance of UIView or a subclass [aView.pin topConstant:0].leftConstant(0).widthIs(100).heightIs(50); ``` -------------------------------- ### Use readable content guide with PinLayout (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Illustrates using PinLayout's `pin.readableMargins` property to align views within the readable content guide. This property exposes `UIView.readableContentGuide` as an `UIEdgeInsets` for easy layout. ```swift label.pin.horizontally(pin.readableMargins) // the label fill horizontally the readable area. view.pin.all(container.pin.readableMargins) // the view fill its parent's readable area. view.pin.left(pin.readableMargins) ``` -------------------------------- ### PinLayout wrapContent() Example (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Shows how to use the `wrapContent()` method in PinLayout to automatically size a view based on its intrinsic content size. This is useful for labels and buttons. ```swift // Example using wrapContent() to size a label label.pin.width(100%).wrapContent() label.text = "This is a long text that needs to wrap." ``` -------------------------------- ### PinLayout autoSizeThatFits() Example (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Demonstrates the `autoSizeThatFits()` method for automatically calculating and setting the size of a view based on its content and a given CGSize constraint. Useful for complex views. ```swift // Example using autoSizeThatFits() for a custom view let targetSize = CGSize(width: 200, height: .greatestFiniteMagnitude) let newSize = customView.pin.autoSizeThatFits(targetSize) customView.pin.size(newSize) ``` -------------------------------- ### Objective-C: Accessing PinLayout Interface Source: https://github.com/layoutbox/pinlayout/blob/master/docs/objective_c.md Demonstrates how to access the PinLayout interface in Objective-C using the `pinObjc` property. This is the equivalent of the `pin` property used in Swift. ```Objective-C view.pinObjc.top().layout(); ``` -------------------------------- ### Start Edge Alignment (LTR/RTL) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Aligns the start edge of a view relative to its superview, respecting language direction (LTR/RTL). Supports pixel offsets, percentage offsets, and UIEdgeInsets. ```APIDOC ## Start Edge Alignment (LTR/RTL) ### Description Aligns the start edge of a view relative to its superview, respecting language direction (LTR/RTL). Supports pixel offsets, percentage offsets, and UIEdgeInsets. ### Methods - `start(_ offset: CGFloat)` - `start(_ offset: Percent)` - `start()` - `start(_ margin: UIEdgeInsets)` ### Details In LTR direction, `offset` specifies the left edge distance from the superview's left edge (in pixels or percentage of superview's width). In RTL direction, `offset` specifies the right edge distance from the superview's right edge (in pixels or percentage of superview's width). `start()` is equivalent to `start(0)`. `start(UIEdgeInsets)` uses `UIEdgeInsets.left` in LTR and `UIEdgeInsets.right` in RTL. ``` -------------------------------- ### PinLayout safeArea, readableMargins, layoutMargins (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Explains and demonstrates the usage of `pin.safeArea`, `pin.readableMargins`, and `pin.layoutMargins` properties in PinLayout for handling screen edges and layout guides. ```swift // Example using safe area, readable margins, and layout margins view.pin.safeArea.top().left().right().bottom() view.pin.readableMargins.vertically(20) view.pin.layoutMargins.horizontally(10) ``` -------------------------------- ### Pin Layout Anchors and Positioning Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Demonstrates various ways to pin and position views using PinLayout's anchor system. Includes examples for top-center, top-left, bottom-right, and centering views relative to other views' anchors. Also shows how to combine multiple anchors for complex layouts and apply margins. ```swift view.pin.topCenter(to: view1.anchor.bottomCenter) view.pin.topLeft(to: view1.anchor.topLeft).bottomRight(to: view1.anchor.center) viewB.pin.topLeft(to: viewA.anchor.topRight) viewB.pin.center(to: viewA.anchor.topRight) viewC.pin.topLeft(to: viewA.anchor.topRight) .bottomRight(to: viewB.anchor.bottomLeft).marginHorizontal(10) viewC.pin.horizontallyBetween(viewA, and: viewB, aligned: .top).height(of: viewA).marginHorizontal(10) ``` -------------------------------- ### PinLayout Relative Positioning Examples (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Demonstrates various ways to use PinLayout's relative positioning methods to position a view relative to others. This includes positioning after, before, and below multiple views, as well as complex arrangements like placing a view between two others with margins. ```swift view.pin.after(of: view4).before(of: view1).below(of: view3) view.pin.after(of: view2) view.pin.below(of: [view2, view3, view4]) ``` ```swift viewC.pin.top().after(of: viewA).before(of: viewB).margin(10) ``` ```swift viewC.pin.top().left(to: viewA.edge.right).right(to: viewB.edge.left). margin(10) ``` ```swift viewC.pin.horizontallyBetween(viewA, and: viewB, aligned: .top).marginHorizontal(10) ``` -------------------------------- ### Complex Relative Layout Example (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Demonstrates a complex layout scenario where a view is positioned below multiple views and constrained by other edges, with specific margins. This showcases the flexibility of combining different PinLayout methods. ```swift a.pin.below(of: [imageView, label], aligned: .left).right(to: label.edge.right).marginTop(10) ``` -------------------------------- ### PinLayout with UITableView Example (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Illustrates how to use PinLayout to manage the layout of cells within a UITableView, particularly when dealing with variable height cells. ```swift // Example of PinLayout usage within a UITableView cell // Assuming 'contentView' is the cell's content view contentView.pin.all().margin(10) contentView.pin.vertically(10).horizontally(10) ``` -------------------------------- ### FlexLayout Benchmark Source Code Example Source: https://github.com/layoutbox/pinlayout/blob/master/docs/Benchmark.md This Swift code snippet illustrates the use of FlexLayout for defining complex UI layouts. It demonstrates how to create nested flex containers, specify item properties like direction, alignment, and margins, and define grow and aspect ratio behaviors. ```swift flex.addItem(contentView).padding(8).define { (flex) in flex.addItem(contentView).padding(8).define { (flex) in flex.addContainer().direction(.row).justifyContent(.spaceBetween).define { (flex) in flex.addItem(actionLabel) flex.addItem(optionsLabel) } flex.addContainer().direction(.row).alignItems(.center).define({ (flex) in flex.addItem(posterImageView).width(50).height(50).marginRight(8) flex.addContainer().grow(1).define({ (flex) in flex.addItem(posterNameLabel) flex.addItem(posterHeadlineLabel) flex.addItem(posterTimeLabel) }) }) flex.addItem(posterCommentLabel) flex.addItem(contentImageView).aspectRatio(350 / 200) flex.addItem(contentTitleLabel) flex.addItem(contentDomainLabel) flex.addContainer().direction(.row).justifyContent(.spaceBetween).marginTop(4).define({ (flex) in flex.addItem(likeLabel) flex.addItem(commentLabel) flex.addItem(shareLabel) }) flex.addContainer().direction(.row).marginTop(2).define({ (flex) in flex.addItem(actorImageView).width(50).height(50).marginRight(8) flex.addItem(actorCommentLabel).grow(1) }) } } ``` -------------------------------- ### PinLayout with UICollectionView Example (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Shows how PinLayout can be applied to UICollectionView cells, enabling flexible layout management for collection views, including variable height cells. ```swift // Example of PinLayout usage within a UICollectionView cell // Assuming 'contentView' is the cell's content view contentView.pin.all() contentView.pin.width(100%).height(100) ``` -------------------------------- ### Adjusting View Width, Height, and Size Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Illustrates how to set the width, height, and overall size of a view using PinLayout. Covers setting dimensions using absolute values (pixels), percentages of the superview, or by matching the dimensions of another view. Also includes examples for setting maximum height. ```swift view.pin.width(100) view.pin.width(50%) view.pin.width(of: view1) view.pin.height(200) view.pin.height(100%).maxHeight(240) view.pin.size(of: view1) view.pin.size(50%) view.pin.size(250) ``` -------------------------------- ### PinLayout Relative Edges Layout Example (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Shows how to use relative edge positioning in PinLayout, allowing views to be positioned relative to the edges of other views or the superview. ```swift // Example of relative edges layout viewA.pin.topLeft(10) viewB.pin.topRight(of: viewA).marginLeft(10) ``` -------------------------------- ### PinLayout Benchmark Source Code Example Source: https://github.com/layoutbox/pinlayout/blob/master/docs/Benchmark.md This Swift code snippet demonstrates how to use PinLayout to arrange UI elements within a view's layoutSubviews method. It utilizes PinLayout's fluent API for defining element positions and margins relative to other elements or the view's edges. ```swift override func layoutSubviews() { super.layoutSubviews() let hMargin: CGFloat = 8 let vMargin: CGFloat = 4 optionsLabel.pin.topRight().margin(hMargin) actionLabel.pin.topLeft().margin(hMargin) posterImageView.pin.below(of: actionLabel, aligned: .left).marginTop(10) posterHeadlineLabel.pin.after(of: posterImageView, aligned: .center).marginLeft(4) posterNameLabel.pin.above(of: posterHeadlineLabel, aligned: .left).marginBottom(vMargin) posterTimeLabel.pin.below(of: posterHeadlineLabel, aligned: .left).marginTop(vMargin) posterCommentLabel.pin.below(of: posterTimeLabel).left(hMargin).marginTop(vMargin) contentImageView.pin.below(of: posterCommentLabel, aligned: .left).right().marginTop(vMargin).marginRight(hMargin) contentTitleLabel.pin.below(of: contentImageView).left().marginHorizontal(hMargin) contentDomainLabel.pin.below(of: contentTitleLabel, aligned: .left) likeLabel.pin.below(of: contentDomainLabel, aligned: .left).marginTop(vMargin) commentLabel.pin.top(to: likeLabel.edge.top).hCenter() shareLabel.pin.top(to: likeLabel.edge.top).right().marginRight(hMargin) actorImageView.pin.below(of: likeLabel, aligned: .left).marginTop(vMargin) actorCommentLabel.pin.after(of: actorImageView, aligned: .center).marginLeft(4) } ``` -------------------------------- ### Pin to Start/End Edges (LTR/RTL Support) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Positions an edge relative to the superview's start or end, automatically adjusting for LTR/RTL language direction. Supports pixel and percentage offsets, as well as UIEdgeInsets for margins. `start()` and `end()` without arguments default to zero offset. ```swift view.pin.start(20) view.pin.end(UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)) ``` -------------------------------- ### Pin View Edges to Superview with Margin (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md This example demonstrates how to pin all four edges of a view (viewA) to its superview's frame with a uniform margin of 10 pixels. It utilizes the `pin.top()`, `pin.bottom()`, `pin.left()`, and `pin.right()` methods. ```swift viewA.pin.top(10).bottom(10).left(10).right(10) ``` -------------------------------- ### PinLayout Percentage Sizing with CGFloat Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Illustrates how to apply percentage-based sizing using a CGFloat variable in Swift with PinLayout. The example shows that the '%' operator can be directly appended to a numeric type to signify a percentage value for layout properties. ```swift let percentageValue: CGFloat = 50 view.pin.width(percentageValue%) ``` -------------------------------- ### Swift: Apply Left Margin in PinLayout Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Applies a left margin of 10 units to a view using PinLayout. This is a basic example showing the direct application of a left margin. ```swift view.pin.left().margin(10) ``` -------------------------------- ### Position View at Top-Left Corner with Margin Source: https://github.com/layoutbox/pinlayout/blob/master/README.md This example shows how to position a view in the top-left corner with specified margins from the top and left edges. It uses the `pin.topLeft()` method with an offset value. ```swift // Position a view at the top left corner with a top and left margin of 10 pixels view.pin.topLeft(10) ``` -------------------------------- ### Objective-C: Explicit layout() Method Source: https://github.com/layoutbox/pinlayout/blob/master/docs/objective_c.md Highlights the necessity of explicitly calling the `layout()` method when using PinLayout in Objective-C to apply layout changes. This differs from Swift where `layout()` is called implicitly. ```Objective-C // Swift // view.pin.width(100) // Objective-c view.pinObjc.width(100).layout(); ``` -------------------------------- ### Complex Layout with PinLayout in Swift Source: https://context7.com/layoutbox/pinlayout/llms.txt Demonstrates a comprehensive layout for a profile header view using various PinLayout features. It positions elements like avatar, labels, buttons, and a stats container, respecting safe area insets and margins. This example highlights advanced positioning and sizing capabilities. ```swift class ProfileHeaderView: UIView { private let backgroundGradient = UIView() private let avatarImage = UIImageView() private let nameLabel = UILabel() private let subtitleLabel = UILabel() private let statsContainer = UIView() private let followButton = UIButton() private let messageButton = UIButton() override func layoutSubviews() { super.layoutSubviews() // Background gradient fills entire view respecting safe area backgroundGradient.pin.all(pin.safeArea) // Avatar: top-left with safe area and margins avatarImage.pin .top(pin.safeArea.top + 20) .left(pin.safeArea.left + 20) .size(80) // Name label: to the right of avatar, auto-sized width nameLabel.pin .after(of: avatarImage, aligned: .top) .right(pin.safeArea.right + 20) .marginLeft(16) .sizeToFit(.width) // Subtitle: below name, aligned left, auto-sized subtitleLabel.pin .below(of: nameLabel, aligned: .left) .right(to: nameLabel.edge.right) .marginTop(4) .sizeToFit(.width) // Buttons: below subtitle, auto-sized content followButton.pin .below(of: subtitleLabel, aligned: .left) .marginTop(12) .sizeToFit(.content) messageButton.pin .after(of: followButton, aligned: .center) .marginLeft(12) .sizeToFit(.content) // Stats container: below avatar and labels, full width statsContainer.pin .below(of: [avatarImage, messageButton]) .horizontally(pin.safeArea) .height(60) .marginTop(20) } } ``` -------------------------------- ### Relative Positioning Between Views with PinLayout (Swift) Source: https://context7.com/layoutbox/pinlayout/llms.txt This Swift code showcases how to position views relative to other views using PinLayout. It includes examples for basic relative positioning, positioning below multiple views, horizontal alignment, RTL-aware placement, and complex multi-view layouts. ```swift override func layoutSubviews() { super.layoutSubviews() let spacing: CGFloat = 12 // Basic relative positioning titleLabel.pin.top(20).horizontally(16).sizeToFit(.width) descriptionLabel.pin.below(of: titleLabel).horizontally(16).marginTop(8).sizeToFit(.width) // Positioned below multiple views (finds lowest one) actionButton.pin.below(of: [titleLabel, descriptionLabel]).right(16).marginTop(20).sizeToFit(.content) // Horizontal relative positioning with vertical alignment iconView.pin.left(16).top(20).size(40) titleLabel.pin.after(of: iconView, aligned: .center).right(16).marginLeft(12).sizeToFit(.width) // RTL-aware relative positioning backButton.pin.before(of: titleLabel, aligned: .center).start(16).sizeToFit(.content) // Complex multi-view layout profileImage.pin.top(pin.safeArea).left(16).size(60).margin(16) nameLabel.pin.after(of: profileImage, aligned: .top).right(16).marginHorizontal(12).sizeToFit(.width) emailLabel.pin.below(of: nameLabel, aligned: .left).right(16).marginTop(4).sizeToFit(.width) separator.pin.below(of: [profileImage, emailLabel]).horizontally().height(1).marginTop(16) } ``` -------------------------------- ### Splitting Long PinLayout Lines in Swift Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Provides an example of how to split a lengthy PinLayout constraint across multiple lines for improved readability. This is useful when defining complex layouts involving multiple edges, margins, and alignment points. ```swift textLabel.pin.below(of: titleLabel) .before(of: statusIcon).after(of: accessoryView) .above(of: button).marginHorizontal(10) ``` -------------------------------- ### Positioning Between Two Views with PinLayout (Swift) Source: https://context7.com/layoutbox/pinlayout/llms.txt This Swift code demonstrates using PinLayout to position views in the space between two other reference views. Examples include horizontal and vertical positioning between elements, and centering a view between two others. ```swift override func layoutSubviews() { super.layoutSubviews() // Position horizontally between header and footer contentView.pin .verticallyBetween(headerView, and: footerView, aligned: .left) .right(16) .marginVertical(20) // Position vertically between left and right sidebars mainPanel.pin .horizontallyBetween(leftSidebar, and: rightSidebar, aligned: .top) .bottom() .marginHorizontal(10) // Center between two elements dividerLine.pin .horizontallyBetween(buttonA, and: buttonB) .vCenter() .width(1) .height(30) } ``` -------------------------------- ### Layout within safe area using PinLayout (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Example demonstrating how to use `UIView.pin.safeArea` to position subviews within the safe area of a view. This ensures content is not obscured by notches or home indicators, and works across various iOS versions. ```swift topTextLabel.pin.top(pin.safeArea.top + 10).hCenter() iconImageView.pin.hCenter().vCenter(-10%) textLabel.pin.below(of: iconImageView).hCenter().width(60%).maxWidth(400).sizeToFit(.width).marginTop(20) scanButton.pin.bottom(pin.safeArea).hCenter().width(80%).maxWidth(300).height(40) ``` -------------------------------- ### Align View to Right of Another with Centered Justification Source: https://github.com/layoutbox/pinlayout/blob/master/README.md This Swift example centers a view (viewB) horizontally in the remaining space to the right of another view (viewA). viewB is given a specific width, and its top edge is aligned with viewA's top. ```swift viewB.pin.after(of: viewA, aligned: .top).right().width(100).justify(.center) ``` -------------------------------- ### Justify and Align Views (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Provides methods to justify views horizontally within available space (left, center, right, start, end) and align them vertically (top, center, bottom). These methods are useful when dimensions and opposing edges are constrained. ```swift view.pin.horizontally().marginHorizontal(20).maxWidth(200).justify(.center) view.pin.below(of: A).above(of: B).width(40).align(.center) ``` -------------------------------- ### PinLayout for Cell Layout Adjustment in Swift Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Demonstrates using PinLayout to adjust view sizes and positions within cells to match container dimensions. Examples cover left alignment, filling remaining space, right alignment, centering, and proportional sizing. ```swift a1.pin.vertically().left().width(50) a2.pin.after(of: a1, aligned: .top).bottomRight().marginLeft(10) b2.pin.vertically().right().width(50) b1.pin.before(of: b2, aligned: .top).bottom().left().marginRight(10) c2.pin.vertically().hCenter().width(50) c1.pin.before(of: c2, aligned: .top).bottom().left().marginRight(10) c3.pin.after(of: c2, aligned: .top).bottom().right().marginLeft(10) d1.pin.vertically().left().width(25%) d2.pin.after(of: d1, aligned: .top).bottom().width(50%).marginLeft(10) d3.pin.after(of: d2, aligned: .top).bottom().right().marginLeft(10) ``` -------------------------------- ### Layout using SafeAreaInsets (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Provides methods to easily integrate with and utilize SafeAreaInsets for layout. It supports iOS 11 and earlier, calculating safe areas using top/bottomLayoutGuides on older versions. Accesses safeAreaInsets, readableContentGuide, layoutMarginsGuide, and keyboardLayoutGuide. ```swift view.pin.all(pin.safeArea) view.pin.top(pin.safeArea) view.pin.left(pin.safeArea.left + 10) view.pin.horizontally(pin.safeArea) button.pin.top(view.pin.safeArea) ``` -------------------------------- ### PinLayout Method Chaining Order in Swift Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Illustrates the recommended order for chaining PinLayout methods to ensure clarity and maintainability. The order `view.pin.[EDGE|ANCHOR|RELATIVE].[WIDTH|HEIGHT|SIZE].[pinEdges()].[MARGINS].[sizeToFit()]` reflects the internal logic of PinLayout, applying edges, then margins, and finally fitting the size. ```swift view.pin.top().left(10%).margin(10, 12, 10, 12) view.pin.left().width(100%).pinEdges().marginHorizontal(12) view.pin.horizontally().margin(0, 12).sizeToFit(.width) view.pin.width(100).height(100%) ``` -------------------------------- ### PinLayout Swift View Layout Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Demonstrates basic view layout using PinLayout in Swift. It shows how to add sublayers and apply top, left, width, and height constraints, as well as relative positioning and sizing based on other views. Assumes a 'view' object and CALayer instances are available. ```swift aLayer = CALayer() bLayer = CALayer() view.layer.addSublayer(aLayer) view.layer.addSublayer(bLayer) ... aLayer.pin.top(10).left(10).width(20%).height(80%) bLayer.pin.below(of: aLayer, aligned: .left).size(of: aLayer) ``` -------------------------------- ### Anchor and Edge Positioning with PinLayout Source: https://context7.com/layoutbox/pinlayout/llms.txt Demonstrates how to position views using a 9-point anchor system or by attaching edges to other views. Supports complex anchor configurations and relative positioning. ```swift override func layoutSubviews() { super.layoutSubviews() // Position using anchors (9 reference points per view) tooltipView.pin.bottomLeft(to: infoButton.anchor.topRight).margin(8).sizeToFit(.content) // Attach edges to other view edges shadowView.pin .left(to: cardView.edge.left) .right(to: cardView.edge.right) .top(to: cardView.edge.bottom) .height(20) // Complex anchor positioning overlayView.pin .topLeft(to: targetView.anchor.topLeft) .bottomRight(to: targetView.anchor.bottomRight) // Center one view on another's corner badge.pin.center(to: avatarView.anchor.topRight).size(20).marginTop(-10).marginRight(-10) } ``` -------------------------------- ### Get Keyboard Area (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Exposes the UIKit UIView.keyboardLayoutGuide value to represent the keyboard's covering area. Useful for adjusting layout when the keyboard is visible. Requires iOS 15+. ```swift container.pin.bottom(view.pin.keyboardArea.top) ``` -------------------------------- ### Automatic Content Sizing with PinLayout Source: https://context7.com/layoutbox/pinlayout/llms.txt Demonstrates how to automatically size views to fit their content or intrinsic size. Supports fitting to width, content, or height while respecting flexible constraints. ```swift override func layoutSubviews() { super.layoutSubviews() // Size label to fit width (adjusts height) titleLabel.pin.top(20).horizontally(16).sizeToFit(.width) // Size to intrinsic content size button.pin.top(100).right(20).sizeToFit(.content) // Flexible sizing with constraints descriptionLabel.pin .below(of: titleLabel) .horizontally(16) .maxHeight(200) .marginTop(8) .sizeToFit(.widthFlexible) // Size to fit height with fixed width cardView.pin .center() .width(300) .sizeToFit(.height) } ``` -------------------------------- ### Responsive Layout with PinLayout in Swift Source: https://context7.com/layoutbox/pinlayout/llms.txt Demonstrates how to create responsive layouts using PinLayout that adapt to different screen widths. It shows conditional layout logic for narrow (vertical stacking) and wide (side-by-side) configurations for a card view, adjusting element positions and sizes based on available space. This ensures optimal UI presentation across devices. ```swift class ResponsiveCardView: UIView { private let imageView = UIImageView() private let titleLabel = UILabel() private let bodyLabel = UILabel() private let actionButton = UIButton() override func layoutSubviews() { super.layoutSubviews() let padding: CGFloat = 16 if frame.width < 400 { // Narrow layout: vertical stacking imageView.pin.top().horizontally().aspectRatio(16/9).margin(padding) titleLabel.pin .below(of: imageView, aligned: .left) .right(padding) .marginTop(padding) .sizeToFit(.width) bodyLabel.pin .below(of: titleLabel, aligned: .left) .right(padding) .marginTop(8) .maxHeight(100) .sizeToFit(.widthFlexible) actionButton.pin .below(of: bodyLabel, aligned: .left) .marginTop(padding) .sizeToFit(.content) } else { // Wide layout: side-by-side imageView.pin .top(padding) .left(padding) .bottom(padding) .width(40%) .aspectRatio(4/3) titleLabel.pin .after(of: imageView, aligned: .top) .right(padding) .marginLeft(padding) .sizeToFit(.width) bodyLabel.pin .below(of: titleLabel, aligned: .left) .right(padding) .marginTop(8) .maxHeight(120) .sizeToFit(.widthFlexible) actionButton.pin .below(of: bodyLabel, aligned: .left) .marginTop(padding) .sizeToFit(.content) } } } ``` -------------------------------- ### Traditional UIView LayoutSubviews and SizeThatFits Source: https://github.com/layoutbox/pinlayout/blob/master/README.md This traditional Swift code demonstrates the implementation of `layoutSubviews` for manual layout and `sizeThatFits` for calculating the view's ideal size. It involves manually setting frames and calculating content size, often requiring duplicated logic. ```swift override func layoutSubviews() { super.layoutSubviews() scrollView.pin.all() imageView.pin.top().horizontally().sizeToFit(.width).margin(margin) textLabel.pin.below(of: imageView).horizontally().sizeToFit(.width).margin(margin) scrollView.contentSize = CGSize(width: scrollView.bounds.width, height: textLabel.frame.maxY + margin) } override func sizeThatFits(_ size: CGSize) -> CGSize { let availableSize = CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude) return CGSize(width: size.width, height: imageView.sizeThatFits(availableSize).height + margin + textLabel.sizeThatFits(availableSize).height + margin ) } ``` -------------------------------- ### Margin Management with PinLayout Source: https://context7.com/layoutbox/pinlayout/llms.txt Explains how to apply margins to views for fine-tuning positioning and sizing. Supports individual, combined, percentage-based, and safe area margins. ```swift override func layoutSubviews() { super.layoutSubviews() // Individual margins view1.pin.top().left().width(100).height(100).marginTop(10).marginLeft(20) // Combined margins view2.pin.bottom().right().size(100).margin(20) // All sides view3.pin.horizontally().top(150).height(60).marginHorizontal(16) // Left & right // Using UIEdgeInsets contentView.pin.all().margin(pin.safeArea) // Respect safe area scrollableContent.pin.all().margin(pin.layoutMargins) // Use layout margins // Percentage margins flexView.pin.center().width(80%).height(60%).margin(10%) // Different margins per side headerView.pin.top().horizontally().height(80).margin(20, 16, 0, 16) // top, left, bottom, right // Vertical and horizontal margins centralView.pin.center().size(200).margin(40, 20) // 40 vertical, 20 horizontal } ``` -------------------------------- ### Animating Layouts with PinLayout (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Demonstrates how to incorporate animations when using PinLayout to change view constraints or positions over time. This allows for smooth transitions and visual feedback. ```swift // Example of animating layout changes with PinLayout UIView.animate(withDuration: 0.3) { view.pin.center().width(100).height(100) view.layoutIfNeeded() } ``` -------------------------------- ### Layout View Below Another View with Alignment (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Positions the current view directly below a specified view or array of views, aligning them horizontally. Supports various horizontal alignment options like left, center, right, start, and end. ```swift viewB.pin.below(of: viewA, aligned: .center) view.pin.below(of: [view2, view3, view4], aligned: .left) ``` -------------------------------- ### Layout View Above Another View with Alignment (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Positions the current view directly above a specified view or array of views, aligning them horizontally. Supports various horizontal alignment options like left, center, right, start, and end. ```swift view.pin.above(of: view2, aligned: .left) view.pin.above(of: [view2, view3, view4], aligned: .left) ``` -------------------------------- ### Fixed and Percentage-Based Sizing with PinLayout Source: https://context7.com/layoutbox/pinlayout/llms.txt Shows how to define view dimensions using fixed pixel values, percentages relative to the superview, or by matching the size of another view. Includes min/max constraints and flexible sizing. ```swift override func layoutSubviews() { super.layoutSubviews() // Fixed dimensions squareView.pin.top(20).left(20).size(100) // 100x100 square rectangleView.pin.top(20).right(20).width(150).height(80) // Percentage-based sizing (relative to superview) halfWidthView.pin.top(150).left().width(50%).height(200) // Match another view's dimensions mirrorView.pin.bottom(20).right(20).size(of: squareView) // Min/max constraints flexibleView.pin .top(300) .horizontally(20) .minHeight(100) .maxHeight(300) .sizeToFit(.heightFlexible) // Mixed percentage and fixed sidePanel.pin.top().bottom().left().width(30%) // 30% width sidebar mainContent.pin.top().bottom().after(of: sidePanel).right() // Remaining space } ``` -------------------------------- ### Alternative Anchor-Based Layout (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Provides an alternative way to achieve a similar layout using anchor points, demonstrating a more direct connection between view anchors for positioning. This can sometimes be more concise for specific alignment needs. ```swift viewB.pin.topCenter(to: viewA.anchor.bottomCenter) ``` -------------------------------- ### Swift: Apply Top, Left, and Right Margins in PinLayout Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Applies top, left, and right margins of 10 units to a view, constrained by a height of 100 units in PinLayout. This example shows a combination of vertical and horizontal margins with a fixed height. ```swift view.pin.top().left().right().height(100).margin(10) ``` -------------------------------- ### Pin View Vertically Between Views with Margins (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Shows how to position a view vertically between two other views, centered relative to the first view, with specified top and bottom margins. This example highlights the use of margin modifiers after defining the primary positioning. ```swift view.pin.verticallyBetween(viewA, and: viewB, aligned: .center).marginVertical(10) ``` -------------------------------- ### Basic Edge Positioning with PinLayout (Swift) Source: https://context7.com/layoutbox/pinlayout/llms.txt This Swift code demonstrates basic edge positioning of views relative to their superview using PinLayout. It covers fixed offsets, percentage-based positioning, safe area insets, RTL-aware alignment, centering, pinning all edges, and pinning horizontal edges. ```swift override func layoutSubviews() { super.layoutSubviews() // Fixed offset from top-left logoView.pin.top(20).left(20).width(100).height(100) // Percentage-based positioning (25% from left edge) sidebarView.pin.left(25%).top().bottom() // Safe area positioning for iPhone X+ notch/home indicator headerView.pin.top(pin.safeArea).horizontally().height(60) // RTL-aware positioning (left in LTR, right in RTL) menuButton.pin.start(16).top(pin.safeArea.top + 10).size(44) // Center positioning loadingSpinner.pin.hCenter().vCenter().size(50) // Pin all edges with margin containerView.pin.all(20) // Pin horizontal edges only bannerView.pin.horizontally(16).top(100).height(80) } ``` -------------------------------- ### Aspect Ratio Handling with PinLayout Source: https://context7.com/layoutbox/pinlayout/llms.txt Illustrates how to maintain aspect ratios for views, either automatically based on image content, explicitly defined, or copied from another view. Supports responsive aspect ratio adjustments. ```swift override func layoutSubviews() { super.layoutSubviews() // UIImageView with automatic aspect ratio from image imageView.pin.top(20).horizontally(20).aspectRatio() // Explicit aspect ratio (width:height = 16:9) videoPlayer.pin.top(100).horizontally(20).aspectRatio(16/9) // Copy aspect ratio from another view thumbnailView.pin.left(20).top(300).width(120).aspectRatio(of: imageView) // Responsive layout maintaining aspect ratio if frame.width < 500 { // Portrait: full width, auto height bannerView.pin.top().horizontally().aspectRatio(2.5) } else { // Landscape: fixed height, auto width bannerView.pin.top().left().height(200).aspectRatio(2.5) } } ``` -------------------------------- ### JavaScript: Fit View Size to Content (Width) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Adjusts the label's width to fit its content while aligning it relative to another view and applying horizontal margins. This demonstrates fitting the width based on content. ```javascript label.pin.after(of: image, aligned: .top).right().marginHorizontal(10).sizeToFit(.width) ``` -------------------------------- ### Layout View After Another View with Alignment (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Positions the current view to the right (in LTR) or left (in RTL) of a specified view or array of views, aligning them vertically. Supports various vertical alignment options like top, center, bottom, start, and end. ```swift view.pin.after(of: view2, aligned: .top) view.pin.after(of: [view2, view3, view4], aligned: .top) ``` -------------------------------- ### Global PinLayout Configuration in Swift Source: https://context7.com/layoutbox/pinlayout/llms.txt Shows how to configure PinLayout behavior globally in a Swift application, typically within the AppDelegate or app initialization. It covers setting layout direction, controlling warning messages, disabling specific warnings like missing layout calls, and configuring iOS safe area inset handling modes. ```swift // In AppDelegate or app initialization func configureLayout() { // Set layout direction Pin.layoutDirection = .auto // .ltr, .rtl, or .auto // Control warning messages Pin.logWarnings = true // Enable/disable all warnings Pin.logMissingLayoutCalls = false // Disable missing layout() warnings // Configure specific warning types Pin.activeWarnings.noSpaceAvailableBetweenViews = true Pin.activeWarnings.aspectRatioImageNotSet = true // iOS 8/9/10 safe area mode Pin.safeAreaInsetsDidChangeMode = .optIn // or .always } ``` -------------------------------- ### Layout View Before Another View with Alignment (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Positions the current view to the left (in LTR) or right (in RTL) of a specified view or array of views, aligning them vertically. Supports various vertical alignment options like top, center, bottom, start, and end. ```swift view.pin.before(of: view2, aligned: .top) view.pin.before(of: [view2, view3, view4], aligned: .top) ``` -------------------------------- ### Immediate Layout in Xcode Playgrounds with PinLayout Source: https://github.com/layoutbox/pinlayout/blob/master/docs/xcode_playground.md Demonstrates how to use the `layout()` method with PinLayout in Swift to force immediate view layout within Xcode Playgrounds. This method is crucial because ARC behaves differently in playgrounds, requiring explicit layout calls. It takes no arguments and returns void. Outside of playgrounds, this method is called implicitly. ```swift view.pin.top(20).bottom(20).width(100).layout() view2.pin.below(of: view).horizontally().layout() ``` -------------------------------- ### Pin View All Edges to Superview with Margin (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md A concise way to pin all edges of a view to its superview with a uniform margin using the `all()` method. This is a shorthand for applying the same offset to all four edges. ```swift view.pin.all(10) ``` -------------------------------- ### Layout View Right of Another View with Alignment (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Positions the current view to the right of a specified view or array of views, aligning them vertically. Supports various vertical alignment options like top, center, bottom, start, and end. Similar to the 'after' method. ```swift view.pin.right(of: view2, aligned: .top) view.pin.right(of: [view2, view3, view4], aligned: .top) ``` -------------------------------- ### Layout View Left of Another View with Alignment (Swift) Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Positions the current view to the left of a specified view or array of views, aligning them vertically. Supports various vertical alignment options like top, center, bottom, start, and end. Similar to the 'before' method. ```swift view.pin.left(of: view2, aligned: .top) view.pin.left(of: [view2, view3, view4], aligned: .top) ``` -------------------------------- ### PinLayout: RTL Language Support Source: https://context7.com/layoutbox/pinlayout/llms.txt Provides support for right-to-left (RTL) languages by automatically adapting layout direction or allowing manual control. Features RTL-aware positioning, margins, anchors, and relative positioning for seamless internationalization. ```swift // Global RTL configuration Pin.layoutDirection = .auto // Auto-detect from system (default) // Pin.layoutDirection = .ltr // Force left-to-right // Pin.layoutDirection = .rtl // Force right-to-left override func layoutSubviews() { super.layoutSubviews() // RTL-aware positioning backButton.pin.start(16).top(60).size(44) // Left in LTR, right in RTL forwardButton.pin.end(16).top(60).size(44) // Right in LTR, left in RTL // RTL-aware margins listItem.pin.all().marginStart(20).marginEnd(48) // Adapts to direction // RTL-aware anchors contextMenu.pin.topStart(to: triggerButton.anchor.bottomStart).margin(4) // RTL-aware relative positioning icon.pin.start(16).top(20).size(32) label.pin.after(of: icon, aligned: .center).end(16).marginStart(12).sizeToFit(.width) } ``` -------------------------------- ### Configure PinLayout Warnings in Swift Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Demonstrates how to individually enable or disable specific warnings within PinLayout. This allows developers to fine-tune the feedback they receive during the layout process, for instance, to suppress warnings about available space or aspect ratio issues. ```swift Pin.activeWarnings.noSpaceAvailableBetweenViews = true Pin.activeWarnings.aspectRatioImageNotSet = false ``` -------------------------------- ### PinLayout Anchors API Source: https://github.com/layoutbox/pinlayout/blob/master/README.md Documentation for using PinLayout's anchor properties to position views relative to other views' anchors. ```APIDOC ## Anchors API ### PinLayout View's Anchors PinLayout adds anchor properties to UIView/NSView for referencing other view's anchors. **Available Anchors:** * `topLeft`, `topCenter`, `topRight` * `topStart`, `topEnd` * `centerLeft`, `centers`, `centerRight` * `centerStart`, `centerEnd` * `bottomLeft`, `bottomCenter`, `bottomRight` * `bottomStart`, `bottomEnd` ### Layout Using Anchors PinLayout can use anchors to position views relative to other views' anchors. **Methods:** * `topLeft(to anchor: Anchor)` * `topCenter(to anchor: Anchor)` * `topRight(to anchor: Anchor)` * `topStart(to anchor: Anchor)` * `topEnd(to anchor: Anchor)` * `centerLeft(to anchor: Anchor)` * `center(to anchor: Anchor)` * `centerRight(to anchor: Anchor)` * `centerStart(to anchor: Anchor)` * `centerEnd(to anchor: Anchor)` * `bottomLeft(to anchor: Anchor)` * `bottomCenter(to anchor: Anchor)` * `bottomRight(to anchor: Anchor)` * `bottomStart(to anchor: Anchor)` * `bottomEnd(to anchor: Anchor)` :pushpin: These methods work even if views do not share the same direct superview, as long as they have a shared ancestor. ``` -------------------------------- ### PinLayout: Layout Completion Source: https://context7.com/layoutbox/pinlayout/llms.txt Explicitly complete layouts using the `.layout()` method, which is required in specific contexts such as animations or Playgrounds. In standard `layoutSubviews()` implementations, layout is applied automatically. ```swift // In animations, explicit layout() call is required UIView.animate(withDuration: 0.3) { self.animatedView.pin.top(100).layout() self.view.layoutIfNeeded() } // In Playgrounds, layout() must be called explicitly view.pin.center().size(100).layout() // Normal usage in layoutSubviews() - layout() not needed override func layoutSubviews() { super.layoutSubviews() view.pin.top(20).left(20).size(100) // Automatic layout application in deinit } ```