• Stars
    star
    836
  • Rank 54,534 (Top 2 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created almost 12 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Making Auto Layout easier to code.

Keep Layout

Keep Layout makes Auto Layout much easier to use from code! No more Interface Builder or Visual Format. Keep Layout provides simple, more readable and powerful API for creating and accessing existing constraints.

Before you start, you should be familiar with Auto Layout topic. How it works and what's the point?

Project Status: All planned features are implemented and functional. Project is maintained and kept up to date with latest iOS/OS X. Feel free to submit Issues or Pull Requests.

Swift Adopters: Swift is fully supported, the usage syntax is mostly the same.

Origins: This library was originally made for this app, especially for its iPad interface.

Attributes

Every view has several attributes that are represented by KeepAttribute class.

  • Dimensions: width, height, aspect ratio
  • Insets to superview: top, bottom, left, right
  • Insets to superview margins: top, bottom, left, right
  • Position in superview: horizontal and vertical
  • Offsets to other views: top, bottom, left, right
  • Alignments with other views: top, bottom, left, right, horizontal, vertical, first & last baselines

They can be accessed by calling methods on UIView/NSView object with one of these format:

@property (readonly) KeepLayout *keep<AttributeName>;
@property (readonly) KeepLayout *(^keep<AttributeName>To)(UIView *)); // Returns block taking another view.

Example:

KeepAttribute *width = view.keepWidth;
KeepAttribute *topOffset = view.keepTopOffsetTo(anotherView); // Invoking the block that returns the actual attribute.

Calling such method for the first time creates the attribute object and any subsequent calls will return the same object. For attributes related to other views this is true for each pair of views. Sometimes even in inversed order or direction:

// aligns are the same regardless of order
viewOne.keepLeftAlign(viewTwo) == viewTwo.keepLeftAlign(viewOne)
// left offset from 1 to 2 is right offset from 2 to 1
viewOne.keepLeftOffset(viewTwo) == viewTwo.keepRightOffset(viewOne)

See KeepView.h for more.

Values

Attributes have three properties: equal, min and max. These are not just plain scalar values, but may have associated a priority.

Priority is Required by default, but you can specify arbitrary priority using provided macros:

KeepValue value = 42; // value 42 at priority 1000
value = 42 +keepHigh; // value 42 at priority 750
value = 42 +keepLow; // value 42 at priority 250
value = 42 +keepFitting; // value 42 at priority 50

// Arbitrary priority:
value = 42 +keepAt(800); // value 42 at priority 800

Priorities are redeclared as KeepPriority using UILayoutPriority values and they use similar naming:

Required > High > Low > Fitting
1000       750    250   50

See KeepTypes.h for more.

Examples

Keep width of the view to be equal to 150:

view.keepWidth.equal = 150;

Keep top inset to superview of the view to be at least 10:

view.keepTopInset.min = 10;

Don't let the first view to get closer than 10 to the second from the left:

firstView.keepLeftOffsetTo(secondView).min = 10;

See the Examples app included in the project for more.


Grouped Attributes

You will often want to set multiple attributes to the same value. For this we have grouped attributes.

You can create groups at your own:

KeepAttribute *leftInsets = [KeepAttribute group:
                             viewOne.keepLeftInset,
                             viewTwo.keepLeftInset,
                             viewThree.keepLeftInset,
                             nil];
leftInsets.equal = 10;

However there are already some accessors to some of them:

view.keepSize    // group of both Width and Height
view.keepInsets  // group of all four insets
view.keepCenter  // group of both axis of position
view.keepEdgeAlignTo  // group of alignments to all four edges

See KeepView.h or KeepAttribute.h for more .

Atomic Groups

Atomic Groups are a way to deactivate multiple attributes at once. With atomic group you can quickly change one desired set of constraints (= layout) to another.

// Create atomic group
KeepAtomic *layout = [KeepAtomic layout:^{
    self.view.keepWidth.min = 320 +keepHigh; // Set minimum width limit.
    self.view.keepVerticalInsets.equal = 0; // Vertically stretch to fit.
}];

[layout deactivate];
// self.view no longer has minimum width of 320 and is no longer stretched vertically.

You can also deactivate them manually using:

self.view.keepWidth.min = KeepNone; // Removes minimum width constraint.
[self.view.keepWidth deactivate]; // Removes all constraints for width.

See KeepAttribute.h for details.

Convenience Methods

For the most used cases there are convenience methods. Nothing you could write yourself, but simplify your code and improve readability. Some of them:

[view keepSize:CGSizeMake(100, 200)];
[view keepInsets:UIEdgeInsetsMake(10, 20, 30, 40)];
[view keepCentered];

See KeepView.h for more.

Array Attributes – What?

Most of the methods added to UIView/NSView class can also be called on any array on views. Such call creates grouped attribute of all contained view attributes:

NSArray *views = @[ viewOne, viewTwo, viewThree ];
views.keepInsets.min = 10;

The above code creates and configures 12 layout constraints!

In addition, arrays allow you to use related attributes more easily, using another convenience methods:

NSArray *views = @[ viewOne, viewTwo, viewThree ];
[views keepWidthsEqual];
[views keepHorizontalOffsets:20];
[views keepTopAligned];

You just created 6 new layout constraints, did you notice?

See NSArray+KeepLayout.h for more.

Animations

Constraints can be animated. You can use simple UIView block animation, but you need to call -layoutIfNeeded at the end of animation block. That triggers -layoutSubviews which applies new constraints.

Or you can use one of the provided methods so you don't need to care:

view.keepWidth.equal = 100;

[view.superview keepAnimatedWithDuration:1 layout:^{
    view.keepWidth.equal = 200;
}];

These are instance methods and must be called on parent view of all affected subviews. At the end of layout block this view receives -layoutIfNeeded method. Any changes to views out of the receiver's subview tree will not be animated.

Spring animation from iOS 7 included, animations on OS X are not supported yet.

See KeepView.h for more.

Layout Guides

KeepLayout adds lazy-loaded invisible .keepLayoutView to every UIViewController in a category. This view is aligned with Top & Bottom Layout Guide and Left & Right Margins of the view controller, which means its size represents visible portion of the view controller. You can use this Layout View to align your views with translucent bars (navigation bar, toolbar, status bar or tab bar).

imageView.keepEdgeAlignTo(controller.keepLayoutView).equal = 0;
// imageView will not be covered by UINavigationBar or UITabBar

See UIViewController+KeepLayout.h for more.

Debugging

Keep Layout uses its own NSLayoutConstraint subclass that overrides -debugDescription method. Once you get error message Unable to simultaneously satisfy constraints., you will see nicely readable description of every constraint you have created. Example:

"<KeepLayoutConstraint:0xc695560 left offset of <YourView 0xc682cf0> to <YourAnotherView 0xc681350> equal to 20 with required priority>",
"<KeepLayoutConstraint:0xc695560 left offset of <YourView 0xc682cf0> to <YourAnotherView 0xc681350> equal to 50 with required priority>",

With this you can very easily find the wrong attribute and fix it.

See KeepLayoutConstraint.h for details.


Implementation Details

Once the attribute is accessed it is created and associated with given view (runtime asociation). In case of related attribbutes, the second view is used as weak key in NSMapTable.
See UIView+KeepLayout.m for details.

KeepValue is declared as _Complex double, which allows seamless convertibility from and to double. The priority is stored as imaginary part.
See KeepTypes.h for details.

Each attribute manages up to three constraints (NSLayoutConstraint) that are created, updated and removed when needed. One constraint for each of three relations (NSLayoutRelation enum) and setting equal, min or max properties modifies them.
See KeepAttribute.m for details.

KeepAttribute class is a class cluster with specific subclasses. One that manages constraints using constant value, one for constraints using multiplier and one grouping subclass that forwards primitive methods to its contained children.
See KeepAttribute.m for details.

Array methods usually call the same selector on contained views and return group of returned attributes.
See NSArray+KeepLayout.m for details.

Animation delay is implemented as real execution delay, not just delay for animating the changes. This differs from UIView block animations and allows you to set up animations in the same update cycle as your initial layout.
See KeepView.m for details.


Version 1.7.0

MIT License, Copyright © 2013-2016 Martin Kiss

THE SOFTWARE IS PROVIDED "AS IS", and so on... see LICENSE.md more.

More Repositories

1

Block-KVO

Objective-C Key-Value Observing made easier with blocks
Objective-C
398
star
2

Objective-Chain

Object-oriented reactive framework, inspired by ReactiveCocoa
Objective-C
237
star
3

UIView-AnimatedProperty

Extending `UIView` block animations to allow you to implement custom animated properties.
Objective-C
158
star
4

Grand-Object-Dispatch

Objective-C wrapper for Grand Central Dispatch
Objective-C
101
star
5

Typed

Brings type inference to Objective-C with almost no hacks.
Objective-C
86
star
6

ITNDescription

Automatic and smart -debugDescription for every Objective-C class.
Objective-C
33
star
7

GrandSwiftDispatch

Not so Grand, not so Swift, and not so Dispatch
Swift
28
star
8

Valid-KeyPath

Allows you to specify Objective-C key-paths using symbols taking all advantages – code-completion, complite-time validation, refactoring.
Objective-C
25
star
9

Essentials

Collection of categories, functions, macros and classes that are essential part of my projects.
Objective-C
18
star
10

MTKButtonState

Allows you to set UIButton state-based properties with dot syntax.
Objective-C
17
star
11

NSObject-GCD

NSObject+GCD: Category on NSObject with methods that wrap some of the GCD functions.
Objective-C
12
star
12

TetrisEngine

Core of a simple Tetris game with no UI
Swift
12
star
13

Where

Locate the user of the current iOS device without CoreLocation.
Objective-C
10
star
14

Yield

Support for multiple entry points and return values in Objective-C.
Objective-C
7
star
15

GEOCoordinateFormatter

NSFormatter for geographic coordinates
Swift
6
star
16

jDateFormat

Never again write date formats by hand. YYYY anyone?
C
4
star
17

LinearKit

Objective-C wrapper for Accelerate framework.
Objective-C
4
star
18

SwiftToolbox

Collection of additions to Swift that I use in every project.
Swift
4
star
19

Adoption

OS X Today Widget to show iOS 9 adoption.
Swift
4
star
20

JSONArchiver

NSCoder that produces JSON object archives, replacement for NSKeyedArchiver.
Objective-C
3
star
21

RichArchiver

Archivation tool for `NSAttributedStrings` for cross-platform exchange – OS X to iOS
Objective-C
3
star
22

CSVBuilder

Swift class that builds CSV files from provided cell data.
Swift
2
star
23

iOS-Toolbox

Open-source app with a collection of tools for iOS devs.
Objective-C
2
star
24

YAML

Attempt to use YAML conveniently from Swift and Objective-C.
Swift
2
star
25

MTKResource

Dealing with iOS devices, screen sizes, languages and extensions.
Objective-C
1
star
26

Objective-R

Not a language.
1
star
27

Geocoder

Geocoding app quickly built with Swift as a tool to inspect address formats around the world.
Swift
1
star
28

Fractioning

Convert decimal numbers to fractions.
Objective-C
1
star
29

SQL-Model

Objective-C modeling library that generates SQL statements.
Objective-C
1
star
30

TRITableModel

Collections modeled to be suitable as UITableView data source.
Objective-C
1
star