Last active
January 13, 2020 19:47
-
-
Save alcidesjunior/995bcea1abb4a5383b992341d9ddb9b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MagicConstraints+UIView.swift | |
// ArctouchQuizChallengeß | |
// | |
// Created by Alcides Junior on 13/01/20. | |
// Copyright © 2020 Alcides Junior. All rights reserved. | |
// | |
//USAGE EXAMPLE | |
startButton | |
.magicTop(16, scoreLabel.bottomAnchor) | |
.magicBottom(16, coverBottomView.bottomAnchor) | |
.magicRight(0, safeArea.trailingAnchor) | |
.magicLeft(0, safeArea.leadingAnchor) | |
// | |
import UIKit | |
extension UIView{ | |
fileprivate func priorityIsntZero(priority: Float)->Bool{ | |
if priority >= 100{ | |
return true | |
} | |
return false | |
} | |
@discardableResult | |
func magicTop(_ padding:CGFloat = 0, _ from:NSLayoutYAxisAnchor? = nil, _ priority: Float = 0) -> Self{ | |
self.translatesAutoresizingMaskIntoConstraints = false | |
if from == nil { | |
let topAnchored = topAnchor.constraint(equalTo: topAnchor, constant: padding) | |
if priorityIsntZero(priority: priority){ | |
topAnchored.priority = UILayoutPriority(rawValue: priority) | |
} | |
topAnchored.isActive = true | |
}else{ | |
if let from = from{ | |
let topAnchored = topAnchor.constraint(equalTo: from, constant: padding) | |
if priorityIsntZero(priority: priority){ | |
topAnchored.priority = UILayoutPriority(rawValue: priority) | |
} | |
topAnchored.isActive = true | |
} | |
} | |
return self | |
} | |
@discardableResult | |
func magicLeft(_ padding:CGFloat = 0, _ from:NSLayoutXAxisAnchor? = nil, _ priority:Float = 100) -> Self{ | |
self.translatesAutoresizingMaskIntoConstraints = false | |
if from == nil { | |
let leftAnchored = leadingAnchor.constraint(equalTo: leadingAnchor, constant: padding) | |
if priorityIsntZero(priority: priority){ | |
leftAnchored.priority = UILayoutPriority(rawValue: priority) | |
} | |
leftAnchored.isActive = true | |
}else{ | |
if let from = from{ | |
let leftAnchored = leadingAnchor.constraint(equalTo: from, constant: padding) | |
if priorityIsntZero(priority: priority){ | |
leftAnchored.priority = UILayoutPriority(rawValue: priority) | |
} | |
leftAnchored.isActive = true | |
} | |
} | |
return self | |
} | |
@discardableResult | |
func magicRight(_ padding:CGFloat = 0, _ from:NSLayoutXAxisAnchor? = nil, _ priority:Float = 100) -> Self{ | |
self.translatesAutoresizingMaskIntoConstraints = false | |
if from == nil { | |
let rightAnchored = trailingAnchor.constraint(equalTo: trailingAnchor, constant: -padding) | |
if priorityIsntZero(priority: priority){ | |
rightAnchored.priority = UILayoutPriority(rawValue: priority) | |
} | |
rightAnchored.isActive = true | |
}else{ | |
if let from = from{ | |
let rightAnchored = trailingAnchor.constraint(equalTo: from, constant: -padding) | |
if priorityIsntZero(priority: priority){ | |
rightAnchored.priority = UILayoutPriority(rawValue: priority) | |
} | |
rightAnchored.isActive = true | |
} | |
} | |
return self | |
} | |
@discardableResult | |
func magicBottom(_ padding:CGFloat = 0, _ from:NSLayoutYAxisAnchor? = nil, _ priority:Float = 100) -> Self{ | |
translatesAutoresizingMaskIntoConstraints = false | |
if from == nil { | |
let bottomAnchored = bottomAnchor.constraint(equalTo: bottomAnchor, constant: -padding) | |
if priorityIsntZero(priority: priority){ | |
bottomAnchored.priority = UILayoutPriority(rawValue: priority) | |
} | |
bottomAnchored.isActive = true | |
}else{ | |
if let from = from{ | |
let bottomAnchored = bottomAnchor.constraint(equalTo: from, constant: -padding) | |
if priorityIsntZero(priority: priority){ | |
bottomAnchored.priority = UILayoutPriority(rawValue: priority) | |
} | |
bottomAnchored.isActive = true | |
} | |
} | |
return self | |
} | |
@discardableResult | |
func magicCenterX(_ padding:CGFloat = 0, _ from:NSLayoutXAxisAnchor? = nil) -> Self{ | |
translatesAutoresizingMaskIntoConstraints = false | |
if from == nil { | |
centerXAnchor.constraint(equalTo: centerXAnchor, constant: padding).isActive = true | |
}else{ | |
if let from = from{ | |
centerXAnchor.constraint(equalTo: from, constant: padding).isActive = true | |
} | |
} | |
return self | |
} | |
@discardableResult | |
func magicCenterY(_ padding:CGFloat = 0, _ from:NSLayoutYAxisAnchor? = nil) -> Self{ | |
translatesAutoresizingMaskIntoConstraints = false | |
if from == nil { | |
centerYAnchor.constraint(equalTo: centerYAnchor, constant: padding).isActive = true | |
}else{ | |
if let from = from{ | |
centerYAnchor.constraint(equalTo: from, constant: padding).isActive = true | |
} | |
} | |
return self | |
} | |
@discardableResult | |
func magicWidth(_ width:CGFloat = 0, _ priority: Float = 100) -> Self{ | |
translatesAutoresizingMaskIntoConstraints = false | |
let widthAnchored = widthAnchor.constraint(equalToConstant: width) | |
if priorityIsntZero(priority: priority){ | |
widthAnchored.priority = UILayoutPriority(rawValue: priority) | |
} | |
widthAnchored.isActive = true | |
return self | |
} | |
@discardableResult | |
func magicHeight(_ height:CGFloat = 0, _ priority: Float = 100) -> Self{ | |
translatesAutoresizingMaskIntoConstraints = false | |
let heightAnchored = heightAnchor.constraint(equalToConstant: height) | |
if priorityIsntZero(priority: priority){ | |
heightAnchored.priority = UILayoutPriority(rawValue: priority) | |
} | |
heightAnchored.isActive = true | |
return self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment