ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - k-o-d-e-n/CGLayout: Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Not Apple Autolayout wrapper. Provides placeholders. Linux support.
Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Not Apple Autolayout wrapper. Provides placeholders. Linux support. - k-o-d-e-n/CGLayout
Visit Site

GitHub - k-o-d-e-n/CGLayout: Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Not Apple Autolayout wrapper. Provides placeholders. Linux support.

GitHub - k-o-d-e-n/CGLayout: Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Not Apple Autolayout wrapper. Provides placeholders. Linux support.

CGLayout

[DEPRECATED] Replaced with new implementation - LayoutUI

Version License Platform

Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Has cross-hierarchy coordinate space. Implementation performed on rect-based constraints.
Fast, asynchronous, declarative, cacheable, extensible. Supported iOS, macOS, tvOS, Linux.

Quick tutorial

Layout with CGLayout built using layout-blocks. To combine blocks into single unit use LayoutScheme entity (or other entities that has suffix Scheme).

let subviewsScheme = LayoutScheme(blocks: [
// ... layout blocks
])

To define block for "view" element use LayoutBlock entity, or just use convenience getter methods func layoutBlock(with:constraints:).

titleLabel.layoutBlock(
    with: Layout(x: .center(), y: .top(5), width: .scaled(1), height: .fixed(120)),
    constraints: [
        logoImageView.layoutConstraint(for: [.bottom(.limit(on: .inner))])
    ]
)
/// or using anchors
titleLabel.layoutBlock(
    with: Layout(x: .center(), y: .top(5), width: .scaled(1), height: .fixed(120)),
    constraints: { anchors in
        anchors.top.equal(to: logoImageView.layoutAnchors.bottom)
    }
)

For understanding how need to built layout block, let's see layout process in LayoutBlock. For example we have this configuration:

LayoutBlock(
    with: layoutElement, 
    layout: Layout(x: .left(10), y: .top(10), width: .boxed(10), height: .boxed(10)),
    constraints: [
        element1.layoutConstraint(for: [
            .bottom(.limit(on: .outer)), .right(.limit(on: .inner))
        ]),
        element2.layoutConstraint(for: [
            .right(.limit(on: .outer)), .bottom(.limit(on: .inner))
        ])
    ]
)

Layout anchors are limiters, that is oriented on frame properties (such as sides, size, position).
Any side-based anchors have three base implementations: alignment, limitation(cropping), pulling. Each this implementation have dependency on working space: inner and outer.
Size-based anchors are represented by two implementations: size, insets.
All layout anchors you can find in enum LayoutAnchor.

To create associated layout constraints use protocol LayoutConstraintProtocol.
Framework provides such default implementations:

  • LayoutConstraint: simple associated constraint that uses var frame of passed element to constrain source rect. Use him to build dependency on external workspace.
  • AdjustLayoutConstraint: associated constraint to adjust size of source space. Only elements conform to protocol AdjustableLayoutElement can use it.
  • ContentLayoutConstraint: associated constraint that uses internal bounds to constrain, defined in 'layoutBounds' property of protocol LayoutElement. Use it if you need to create dependency on internal workspace. For example, element inside UIScrollView.
  • AnonymConstraint: constraint to restrict source space independently from external environment.
  • MutableLayoutConstraint: Layout constraint that creates possibility to change active state.
    You can find all this constraints through convenience functions in related elements. Use him to build layout blocks.

In common case, adjust constraints should be apply after any other constraints (but not always).

weatherLabel.layoutBlock(
    with: Layout(x: .left(10), y: .top(), width: .scaled(1), height: .scaled(1)),
    constraints: [
        weatherImageView.layoutConstraint(for: [.top(.limit(.inner)), .right(.limit(.outer)), .height()]),
        weatherLabel.adjustLayoutConstraint(for: [.width()])
    ]
)
AnonymConstraint(anchors: [
    Inset(UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 15))
])

For implementing custom layout entities and save strong typed code, use static func build(_ base: Conformed) -> Self method.

Each layout-block has methods for layout, take snapshot and applying snapshot. Consequently you may use layout-blocks for direct layout, background layout and cached layout:

// layout directly
layoutScheme.layout()

// layout in background
let bounds = view.bounds
    DispatchQueue.global(qos: .background).async {
    let snapshot = self.layoutScheme.snapshot(for: bounds)
    DispatchQueue.main.sync {
        self.layoutScheme.apply(snapshot: snapshot)
    }
}

// cached layout
if UIDevice.current.orientation.isPortrait, let snapshot = portraitSnapshot {
    layoutScheme.apply(snapshot: snapshot)
} else if UIDevice.current.orientation.isLandscape, let snapshot = landscapeSnapshot {
    layoutScheme.apply(snapshot: snapshot)
} else {
    layoutScheme.layout()
}

Typical implementation sizeThatFits(_:) method

func sizeThatFits(_ size: CGSize) -> CGSize {
    let sourceRect = CGRect(origin: .zero, size: size)
    let snapshot = scheme.snapshot(for: sourceRect)
    return snapshot.frame
}

LayoutGuide

Framework provides LayoutGuide as analogue UILayoutGuide. It has possible to generate views and add them to hierarchy.
LayoutGuide can used as invisible limiter and also as layout container.
Default layout containers:

  • StackLayoutGuide - simple implementation of stack.
  • ScrollLayoutGuide - has similar interface with UIScrollView. By use him we can enable scrolling absolutely everywhere.
  • LayoutPlaceholder - single element container that can load view lazily. Has default implementations for CALayer - LayerPlaceholder and UIView - ViewPlaceholder.
  • UIViewPlaceholder - single element container based on UILayoutGuide.

UILayouGuide also adopts LayoutElement protocol. Therefore you can safely build constraints based on UIView.safeAreaLayoutGuide and others.

RTL

To enable Righ-to-Left mode use global configuration:

CGLConfiguration.default.isRTLMode = true

For more details, see documentation and example project.

Code documentation

See here

Example

macOS, iOS, tvOS

To run the example project, clone the repo, and run pod install from the Example directory first.

Linux

To run the example project, clone the repo, and run swift run from the linux_example directory first.

Requirements

Swift 5

Installation

CGLayout is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "CGLayout"

Contributing

I will be happy your feedback, advices and pull requests. For more information read here

Author

Denis Koryttsev Email: [email protected] Twitter: https://twitter.com/K_o_D_e_N

License

CGLayout is available under the MIT license. See the LICENSE file for more info.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory