FAAutoLayout

所属分类:硬件设计
开发工具:Swift
文件大小:0KB
下载次数:0
上传日期:2019-12-03 16:00:53
上 传 者sh-1993
说明:  以编程方式创建NSLayoutConstraints的简单直观方法,
(A simple and intuitive way to create NSLayoutConstraints programmatically,)

文件列表:
.swift-version (4, 2019-06-05)
.travis.yml (577, 2019-06-05)
Example/ (0, 2019-06-05)
Example/FAAutoLayout.xcodeproj/ (0, 2019-06-05)
Example/FAAutoLayout.xcodeproj/project.pbxproj (29024, 2019-06-05)
Example/FAAutoLayout.xcodeproj/project.xcworkspace/ (0, 2019-06-05)
Example/FAAutoLayout.xcodeproj/project.xcworkspace/contents.xcworkspacedata (157, 2019-06-05)
Example/FAAutoLayout.xcodeproj/project.xcworkspace/xcshareddata/ (0, 2019-06-05)
Example/FAAutoLayout.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist (238, 2019-06-05)
Example/FAAutoLayout.xcodeproj/xcshareddata/ (0, 2019-06-05)
Example/FAAutoLayout.xcodeproj/xcshareddata/xcschemes/ (0, 2019-06-05)
Example/FAAutoLayout.xcodeproj/xcshareddata/xcschemes/FAAutoLayout-Example.xcscheme (4476, 2019-06-05)
Example/FAAutoLayout.xcworkspace/ (0, 2019-06-05)
Example/FAAutoLayout.xcworkspace/contents.xcworkspacedata (230, 2019-06-05)
Example/FAAutoLayout.xcworkspace/xcshareddata/ (0, 2019-06-05)
Example/FAAutoLayout.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist (238, 2019-06-05)
Example/FAAutoLayout/ (0, 2019-06-05)
Example/FAAutoLayout/AppDelegate.swift (2185, 2019-06-05)
Example/FAAutoLayout/Base.lproj/ (0, 2019-06-05)
Example/FAAutoLayout/Base.lproj/LaunchScreen.xib (3706, 2019-06-05)
Example/FAAutoLayout/Base.lproj/Main.storyboard (5586, 2019-06-05)
Example/FAAutoLayout/DetailViewController.swift (1197, 2019-06-05)
Example/FAAutoLayout/Images.xcassets/ (0, 2019-06-05)
Example/FAAutoLayout/Images.xcassets/AppIcon.appiconset/ (0, 2019-06-05)
Example/FAAutoLayout/Images.xcassets/AppIcon.appiconset/Contents.json (586, 2019-06-05)
Example/FAAutoLayout/Info.plist (1149, 2019-06-05)
Example/FAAutoLayout/ViewController.swift (2645, 2019-06-05)
Example/Podfile (209, 2019-06-05)
Example/Podfile.lock (280, 2019-06-05)
Example/Pods/ (0, 2019-06-05)
Example/Pods/Local Podspecs/ (0, 2019-06-05)
Example/Pods/Local Podspecs/FAAutoLayout.podspec.json (642, 2019-06-05)
Example/Pods/Manifest.lock (280, 2019-06-05)
Example/Pods/Pods.xcodeproj/ (0, 2019-06-05)
Example/Pods/Pods.xcodeproj/project.pbxproj (43406, 2019-06-05)
Example/Pods/Pods.xcodeproj/project.xcworkspace/ (0, 2019-06-05)
Example/Pods/Pods.xcodeproj/project.xcworkspace/contents.xcworkspacedata (135, 2019-06-05)
Example/Pods/Pods.xcodeproj/xcshareddata/ (0, 2019-06-05)
Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/ (0, 2019-06-05)
... ...

# FAAutoLayout [![CI Status](http://img.shields.io/travis/fabioalmeida/FAAutoLayout.svg?style=flat)](https://travis-ci.org/fabioalmeida/FAAutoLayout) [![Version](https://img.shields.io/cocoapods/v/FAAutoLayout.svg?style=flat)](http://cocoapods.org/pods/FAAutoLayout) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![License](https://img.shields.io/cocoapods/l/FAAutoLayout.svg?style=flat)](http://cocoapods.org/pods/FAAutoLayout) [![Platform](https://img.shields.io/cocoapods/p/FAAutoLayout.svg?style=flat)](http://cocoapods.org/pods/FAAutoLayout) FAAutoLayout is a compact [Auto Layout](https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/index.html) library for iOS. It's built on top of [NSLayoutConstraint](https://developer.apple.com/documentation/uikit/nslayoutconstraint), extending the capabilities of UIView allowing developers to intuitively create constraints programmatically - the same way they do it using the interface builder. This lightweight library was designed thinking about the people who like to create their interfaces without using storyboards of XIB files, helping to remove the excess of boilerplate code when it comes to defining the layout constraints. The naming used was thought to make the library adoption super fast to everyone that is already used to add constraints using the interface builder. For more detailed examples please check the [Usage](#usage) section. ## Requirements The minimum requirements needed to use `FAAutoLayout` are: - iOS 8.0 - Xcode 8 - Obj-C and Swift compatible ## Installation ### CocoaPods You can use [CocoaPods](http://cocoapods.org) to add `FAAutoLayout` to your project. To install it, simply specify it in your **Podfile**: ```ruby source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' use_frameworks! target 'TargetName' do pod 'FAAutoLayout', '~> 0.4.1' end ``` After specifying the new dependency on the Podfile, just run `pod install` to make sure you install the latest version of the library. ### Carthage [Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate `FAAutoLayout` into your Xcode project using Carthage, specify it in your `Cartfile`: ```ogdl github "fabioameida/FAAutoLayout" ~> 0.4.1 ``` Run `carthage update` to build the framework and drag the built `FAAutoLayout.framework` into your Xcode project. ## Usage To import the library to your project just add the following line to your Swift file: ```swift import FAAutoLayout ``` If you have a view that you want to add to your UIViewController and make it fill the container in all the margins, you just need to do the following: ```swift let backgroundView = UIView() backgroundView.backgroundColor = .lightGray self.view.addSubview(backgroundView) // with only one line of code all the 4 needed constraints are automatically added to the view backgroundView.fillContainer() ``` For adding a view with a fixed width and height that sits on the top-left corner of the view: ```swift let topLeftView = UIView() topLeftView.backgroundColor = .blue backgroundView.addSubview(topLeftView) topLeftView.constrainLeadingSpaceToContainer() topLeftView.constrainTopSpaceToContainer(10) topLeftView.constrainWidth(150) topLeftView.constrainHeight(45) ``` If we want to define relations between two views on the same hierarchy level (i.e. share the same `superview`) we can define, for instance, one horizontal spacing constraint between them: ```swift let topRightView = UIView() topRightView.backgroundColor = .red backgroundView.addSubview(topRightView) topRightView.constrainHorizontalSpacing(toView: topLeftView, constant: 20) topRightView.constrainTopSpaceToContainer(10) topRightView.constrainTrailingSpaceToContainer(30) topRightView.constrainEqualHeight(toView: topLeftView, constant: 0, relation: .equal, priority: UILayoutPriorityRequired, multiplier: 2) ``` Note that in the previous example the height was not a fixed value but calculated using the first view height and a multiplier. It's always possible to store an added constraint to be later changed: ```swift let heightConstraint = topLeftView.constrainHeight(45) // (...) heightConstraint.constant = 80 ``` On the previous examples, lots of constraints were being added in relation with a container view. Nevertheless, it is also possible to create these relations to other view that is above on the container view hierarchy chain: ```swift // container view that will sit below topLeftView let containerView1 = UIView() containerView1.backgroundColor = .green backgroundView.addSubview(containerView1) containerView1.constrainWidth(200) containerView1.constrainHeight(200) containerView1.constrainVerticalSpacing(toView: topLeftView, constant: 100) containerView1.constrainLeadingSpaceToContainer(10) // view that will adapt to containerView1 let containerView2 = UIView() containerView2.backgroundColor = .black containerView1.addSubview(containerView2) containerView2.fillContainer(40) // view that that despite being inside containerView2, will adapt to containerView1 let containerView3 = UIView() containerView3.backgroundColor = .orange containerView3.alpha = 0.5 containerView2.addSubview(containerView3) // here, even though containerView2 is the container for containerView3, we can make a relation to containerView1 since it the the superview of containerView2 containerView3.fill(view: containerView1, constant: 20) ``` The final result would be something similar to the following image: ![example1](https://user-images.githubusercontent.com/474965/27745013-ba946316-5db9-11e7-80be-b026f0ada95c.png) In some other cases we need to create our views programmatically because their number might be dynamic depending on some business logic. On the next example you can see 2 `UIView` class methods that allow to add constraints to an Array of UIViews with one single function call: ```swift let dynamicNumberOfViews = 5 var dynamicViews = [UIView]() for _ in 0..
近期下载者

相关文件


收藏者