### Implementing the `Layoutable` Protocol Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Provides an example of how to conform to the `Layoutable` protocol, which is analogous to SwiftUI's `View`. This involves implementing an `activation` property and a `layout` property, and calling `updateLayout()` to apply the layout. ```Swift class SomeView: UIView, Layoutable { var activation: Activation? @LayoutBuilder var layout: some Layout { self.sl.sublayout { // ... } } init(frame: CGRect) { super.init(frame: frame) self.sl.updateLayout() // call active or update of Layout } } ``` -------------------------------- ### Install SwiftLayout via Swift Package Manager Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Provides the necessary dependency declaration for integrating SwiftLayout into a Swift project using Swift Package Manager (SPM). This snippet specifies the package URL and the minimum version requirement. ```Swift dependencies: [ .package(url: "https://github.com/ioskrew/SwiftLayout", from: "4.0.0"), ], ``` -------------------------------- ### Basic Usage of SwiftLayout's LayoutBuilder for View Hierarchy Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Illustrates the fundamental use of `@LayoutBuilder` to define a nested view hierarchy, simplifying `addSubview` calls into a declarative DSL. This example shows how to add subviews and sub-subviews in a concise, readable format. ```Swift @LayoutBuilder var layout: some Layout { view.sl.sublayout { subview.sl.sublayout { subsubview subsub2view } } } ``` -------------------------------- ### Integrating SwiftLayout with SwiftUI Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Provides an example of how to integrate a `UIView` or `UIViewController` that implements `Layoutable` into a SwiftUI view hierarchy. It shows how to use the `.sl.swiftUI` modifier to embed the SwiftLayout-managed view within SwiftUI, enabling previews as well. ```Swift class ViewUIView: UIView, Layoutable { var layout: some Layout { ... } } ... struct SomeView: View { var body: some View { VStack { ... ViewUIView().sl.swiftUI ... } } } struct ViewUIView_Previews: PreviewProvider { static var previews: some Previews { ViewUIView().sl.swiftUI } } ``` -------------------------------- ### Activating Layouts for Updates with `active()` Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Explains how to use the `active()` method to obtain an `Activation` object. This object contains information necessary for updating the layout later, allowing for dynamic changes based on conditions. ```Swift @LayoutBuilder func layout() -> some Layout { superview.sl.sublayout { selfview.sl.anchors { if someCondition { Anchors.bottom } else { Anchors.top } } } } var activation: Activation init() { activation = layout().active() } func someUpdate() { activation = layout().update(fromActivation: activation) } ``` -------------------------------- ### Nesting LayoutBuilder and AnchorsBuilder Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Illustrates how to use `LayoutBuilder` and `AnchorsBuilder` together to add subviews and apply autolayouts in a nested, hierarchical structure, enabling complex UI compositions. ```Swift @LayoutBuilder func layout() -> some Layout { superview.sl.sublayout { selfview.sl.anchors { Anchors.allSides }.sublayout { subview.sl.anchors { Anchors.allSides } } } } ``` -------------------------------- ### Applying Constraints with `equalTo` Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Shows how to set a second item and its attribute for a constraint using the `equalTo` method within a `sublayout` block. This method allows specifying a constant offset, effectively creating a constraint like `selfview.top = superview.top + 10`. ```Swift superview.sl.sublayout { selfview.sl.anchors { Anchors.top.equalTo(superview, attribute: .top, constant: 10) } } ``` ```Constraint Expression selfview.top = superview.top + 10 ``` -------------------------------- ### Activating Layouts Instantly with `finalActive()` Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Shows how to use the `finalActive()` method on a `Layout` object to immediately add subviews and activate all defined constraints. This is suitable for layouts that do not require subsequent updates. ```Swift @LayoutBuilder func layout() -> some Layout { superview.sl.sublayout { selfview.sl.anchors { Anchors.top } } } init() { layout().finalActive() } ``` -------------------------------- ### Define Complex UI Layouts with SwiftLayout's DSL Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Demonstrates how to use SwiftLayout's `@LayoutBuilder` and `Anchors` DSL to define a complex view hierarchy and apply Auto Layout constraints programmatically. It shows adding subviews, setting leading, trailing, center, and size constraints relative to superviews and other views, including identifying views for constraint referencing. ```Swift @LayoutBuilder var layout: some Layout { self.sl.sublayout { leftParenthesis.sl.anchors { Anchors.leading.equalToSuper(constant: 16) Anchors.centerY } viewLogo.sl.anchors { Anchors.leading.equalTo(leftParenthesis, attribute: .trailing, constant: 20) Anchors.centerY.equalToSuper(constant: 30) Anchors.size.equalTo(width: 200, height: 200) } UIImageView().sl.identifying("plus").sl.onActivate { imageView in imageView.image = UIImage(systemName: "plus") imageView.tintColor = .SLColor }.anchors { Anchors.center.equalToSuper(yOffset: 30) Anchors.size.equalTo(width: 150, height: 150) } constraintLogo.sl.anchors { Anchors.trailing.equalTo(rightParenthesis.leadingAnchor) Anchors.centerY.equalTo("plus") Anchors.size.equalTo(width: 200, height: 150) } rightParenthesis.sl.anchors { Anchors.trailing.equalToSuper(constant: -16) Anchors.centerY } } } ``` -------------------------------- ### Animating Layout Changes with `UIView.animate` Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Shows how to animate layout changes by calling `self.sl.updateLayout(forceLayout: true)` inside a `UIView.animate` block. This allows for smooth transitions when constraints or view hierarchies are modified dynamically, providing a fluid user experience. ```Swift final class PreviewView: UIView, Layoutable { var capTop = true { didSet { // start animation for change constraints UIView.animate(withDuration: 1.0) { self.sl.updateLayout(forceLayout: true) } } } // or just use the convenient propertyWrapper like below // @AnimatableLayoutProperty(duration: 1.0) var capTop = true let capButton = UIButton() let shoeButton = UIButton() let titleLabel = UILabel() var topView: UIButton { capTop ? capButton : shoeButton } var bottomView: UIButton { capTop ? shoeButton : capButton } var activation: Activation? var layout: some Layout { self.sl.sublayout { topView.sl.anchors { Anchors.cap } bottomView.sl.anchors { Anchors.top.equalTo(topView.bottomAnchor) Anchors.height.equalTo(topView) Anchors.shoe } titleLabel.sl.onActivate { label in label.text = "Top Title" UIView.transition(with: label, duration: 1.0, options: [.beginFromCurrentState, .transitionCrossDissolve]) { label.textColor = self.capTop ? .black : .yellow } }.anchors { Anchors.center.equalTo(topView) } UILabel().sl.onActivate { label in label.text = "Bottom Title" label.textColor = self.capTop ? .yellow : .black }.identifying("title.bottom").anchors { Anchors.center.equalTo(bottomView) } } } override init(frame: CGRect) { super.init(frame: frame) initViews() } required init?(coder: NSCoder) { super.init(coder: coder) initViews() } func initViews() { capButton.backgroundColor = .yellow shoeButton.backgroundColor = .black capButton.addAction(.init(handler: { [weak self] _ in self?.capTop.toggle() }), for: .touchUpInside) shoeButton.addAction(.init(handler: { [weak self] _ in self?.capTop.toggle() }), for: .touchUpInside) self.sl.updateLayout() } } ``` -------------------------------- ### Applying Multiplier to Anchors Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Demonstrates how to apply a multiplier to an anchor constraint, which scales the second item's attribute in the constraint equation. ```Swift Anchors.top.multiplier(10) ``` -------------------------------- ### Accessing Anchor Properties Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Demonstrates how to access predefined anchor properties like `top` and `bottom` directly from the `Anchors` enum, representing a set of layout attributes. ```Swift Anchors.top.bottom ``` -------------------------------- ### Referencing Views by Identifier with `identifying` Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Demonstrates how to assign an identifier to a view using `identifying` and then reference it in layout constraints instead of using the direct view reference. This can improve readability and debugging, as the identifier is also included in `NSLayoutConstraint` descriptions. ```Swift contentView.sl.sublayout { nameLabel.sl.identifying("name").sl.anchors { Anchors.cap } ageLabel.sl.anchors { Anchors.top.equalTo("name", attribute: .bottom) Anchors.shoe } } ``` -------------------------------- ### Separating Complex Layout Hierarchies Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Demonstrates how to break down a complex layout hierarchy into separate `sublayout` blocks. This approach improves code organization and readability, especially for intricate UI structures. ```Swift @LayoutBuilder func layout() -> some Layout { superview.sl.sublayout { selfview.sl.anchors { Anchors.allSides } } selfview.sl.sublayout { subview.sl.anchors { Anchors.allSides } } } ``` -------------------------------- ### Setting Self Width and Height Constraints Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Shows how to set `width` and `height` constraints for the view itself when no second item is specified. This effectively sets fixed dimensions for the view. ```Swift superview.sl.sublayout { selfview.sl.anchors { Anchors.width.height.equalTo(constant: 10) // only for selfview } } ``` ```Constraint Expression selfview.width = 10 selfview.height = 10 ``` -------------------------------- ### Automatic Layout Updates with `@LayoutProperty` Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Illustrates the use of the `@LayoutProperty` wrapper to automatically call `updateLayout` when the wrapped property's value changes. This simplifies dynamic layout management by removing the need for manual `didSet` observers. ```Swift @LayoutProperty var showMiddleName: Bool = false // change value call updateLayout of Layoutable var layout: some Layout { self.sl.sublayout { firstNameLabel if showMiddleName { middleNameLabel } lastNameLabel } } ``` -------------------------------- ### Decorating Views with `onActivate` Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Explains how to use the `onActivate` closure to perform view decoration and modification during the layout activation process. The closure is called when the view is activated, allowing for setting properties like text and color. ```Swift contentView.sl.sublayout { nameLabel.sl.onActivate { label in label.text = "Hello" label.textColor = .black }.anchors { Anchors.allSides } } ``` -------------------------------- ### Matching Parent Attributes with Anchors Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Illustrates how to configure child attributes to implicitly match corresponding parent item attributes when no specific relation function is provided. This simplifies setting multiple constraints like `top` and `bottom` to match the parent's bounds. ```Swift superview.sl.sublayout { selfview.sl.anchors { Anchors.sl.top.bottom } } ``` ```Constraint Expression selfview.top = superview.top selfview.bottom = superview.bottom ... ``` -------------------------------- ### Manually Updating Layout with `updateLayout` Source: https://github.com/ioskrew/swiftlayout/blob/main/README.md Demonstrates how to manually trigger a layout update by calling `self.sl.updateLayout()` within a `didSet` observer when a property affecting the layout's conditional elements changes. This ensures the UI reflects the current state of the layout. ```Swift var showMiddleName: Bool = false { didSet { self.sl.updateLayout() } } var layout: some Layout { self.sl.sublayout { firstNameLabel if showMiddleName { middleNameLabel } lastNameLabel } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.