Giter Site home page Giter Site logo

tayphoon / oalayoutanchor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nsomar/oalayoutanchor

0.0 2.0 0.0 247 KB

Porting NSLayoutAnchor to iOS7 (full port)

License: MIT License

Objective-C 95.78% Swift 0.18% Ruby 0.43% C 0.49% Shell 3.12%

oalayoutanchor's Introduction

OALayoutAnchor

Build Status Version License Platform

OALayoutAnchor is an effort to port the amazing NSLayoutAnchor introduced in iOS 9 to iOS 7.

OALayoutAnchor is a 100% port of NSLayoutAnchor to iOS 7, it aims to provide the same functionality to iOS 7, and rollback to NSLayoutAnchor on iOS 9 and up.

OALayoutGuide is a port of UILayoutGuide to iOS 8, it aims to provide the same functionality to iOS 8, and rollback to UILayoutGuide on iOS 9 and up. Warning this version works only since iOS 8 because NSLayoutAttributeMargins will be avalible since iOS 8.

Usage

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

Usage is 100% compatible with NSLayoutAnchors.:
Example Usage (Taken from apple websites):

// Creating constraints using NSLayoutConstraint
[NSLayoutConstraint
 constraintWithItem:subview
 attribute:NSLayoutAttributeLeading
 relatedBy:NSLayoutRelationEqual
 toItem:self.view
 attribute:NSLayoutAttributeLeadingMargin
 multiplier:1.0
 constant:0.0].active = YES;
 
[NSLayoutConstraint
 constraintWithItem:subview
 attribute:NSLayoutAttributeTrailing
 relatedBy:NSLayoutRelationEqual
 toItem:self.view
 attribute:NSLayoutAttributeTrailingMargin
 multiplier:1.0
 constant:0.0].active = YES;
 

The following constraint setup can be rewritten to the following:

// Creating the same constraints using Layout Anchors
UILayoutGuide *margin = self.view.layoutMarginsGuide;
 
[subview.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor].active = YES;
[subview.trailingAnchor constraintEqualToAnchor:margin.trailingAnchor].active = YES;

For iOS 7, since active property is not available on NSLayoutConstraint the constraint set will be already installed. The bellow code shows the usage on iOS7:

// Creating the same constraints using Layout Anchors
UILayoutGuide *margin = self.view.layoutMarginsGuide;
 
[subview.leadingAnchor constraintEqualToAnchor:margin.leadingAnchor];
[subview.trailingAnchor constraintEqualToAnchor:margin.trailingAnchor];

For a better documentation please refer to apple docs.

Installation

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

pod "OALayoutAnchor"

Tests

There are several unit tests covering most of the Equality constraints. Unit test for covering Greater than or Lesser than are coming next.

Test are currently running for iOS 8 only, test for iOS 7 are coming next.

The following a human readable text subscript (generated with specipy).

Notes on usage in iOS 7

Since iOS7 NSLayoutConstraint does not have an active property, the category NSLayoutConstraint+SuppressActive was introduced to make the usage of the constraints transparent between iOS7 and 8.

When using the bellow line:

[label1.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:20].active = YES;

On iOS 8, the returned constraint is not active until we call .active = YES on it. However, on iOS 7, since there is no active property on NSLayoutConstraint. a method has been attached to the NSLayoutConstraint to ignore the isActive method calls.

That means that calling isActive or active on iOS 7 will not crash the app, these calls will just be ignored.

Notes on usage in swift

Since anchor UIView's properties are annotated with NS_AVAILABLE_IOS(9_0);, it makes them unusable with swift when the app deployment target is less than iOS9. The compiler will not allow the usage of anchor properties without guarding against the iOS version.

if #available(iOS 9.0, *) {
    view.leftAnchor
} else {
    // Fallback on earlier versions
}

In order to target this problem, OALayoutAnchor contains a mirror of all the anchor properties. these properties are named with a prefix of oa_

var oa_leadingAnchor: NSLayoutXAxisAnchor! { get }
var oa_trailingAnchor: NSLayoutXAxisAnchor! { get }
var oa_leftAnchor: NSLayoutXAxisAnchor! { get }
... etc..

As an example, using oa_leadingAnchor in iOS 9 and up, will forward the invocation to leadingAnchor. On iOS 8 and 7, this call will use the layout anchor from OALayourAnchor.

NSLayoutConstraint active property is also unusable in swift for any iOS less than 8. In order to address this issue please use oa_active. Using oa_active will result in using active on iOS 8,9 and doing nothing on iOS 7.

This is an example in swift:

view1.oa_widthAnchor.constraintEqualToConstant(100).oa_active = true
view1.oa_heightAnchor.constraintEqualToConstant(100).oa_active = true
view1.oa_centerXAnchor.constraintEqualToAnchor(self.view.oa_centerXAnchor).oa_active = true
view1.oa_centerYAnchor.constraintEqualToAnchor(self.view.oa_centerYAnchor).oa_active = true

Future improvements

The following would be nice to have for future versions

  • Increase the test coverage for Greater than and Less Than
  • Better Documentation
  • Add tests for iOS 7
  • Add support OALayoutGuide for iOS 7

Author

Omar Abdelhafith, [email protected]

License

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.