Auto Layout made easy with the Custom Layout.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ geminstallcocoapodsTo integrate CustomLayout into your Xcode project using CocoaPods, specify it in your Podfile:
source'https://github.com/CocoaPods/Specs.git'platform:ios,'12.0'use_frameworks!target'<Your Target Name>'dopod'CustomLayout','~> 1.0.1'endThen, run the following command:
$ podinstallSwift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.
Xcode 11+ is required to build CustomLayout using Swift Package Manager.
dependencies:[.package(url:"https://github.com/MnkGitBox/CustomLayout.git",.upToNextMajor(from:"1.0.1"))]If you prefer not to use either of the aforementioned dependency managers, you can integrate CustomLayout into your project manually.
You can call layout.activate method provided by the Custom Layout library.
- Without Custom Layout
view.addSubview(redView)
view.translatesAutoresizingMaskIntoConstraints =falseNSLayoutConstraint.activate([redView.leadingAnchor.constraint(equalTo: view.leadingAnchor,
constant:50),
redView.trailingAnchor.constraint(equalTo: view.trailingAnchor,
constant:-50),
redView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,
constant:40),
redView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,
constant:-20)])- With Custom Layout
view.addSubview(redView)
redView.layout.activate([.leading(constant:50),.traling(constant:-50),.top(constant:40, safeAreaAlign:true),.bottom(constant:-20, safeAreaAlign:true)])Custom Layout not only helps to layout subviews to its super view. We can use it to layout view components with other views who share the same ancestors.
greenView.layout.activate(to:[view :[.bottom(constant:-2, safeAreaAlign:true),.traling(constant:-20)],
redView :[.leadingToTraling(constant:-10),.topToBottom(constant:20)]])You can find the original sample code here.
- Malith Nadeeshan (malithnadeeshan)
CustomLayout is released under the MIT license. See LICENSE for details.
