You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.9 KiB
99 lines
3.9 KiB
//
|
|
// UIView+Extensions.swift
|
|
// TournamentStats
|
|
//
|
|
// Created by Laurent Morvillier on 05/08/2019.
|
|
// Copyright © 2019 Stax River. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
extension UIView {
|
|
|
|
func toImage() -> UIImage? {
|
|
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0.0)
|
|
if let context = UIGraphicsGetCurrentContext() {
|
|
self.layer.render(in: context)
|
|
if let image = UIGraphicsGetImageFromCurrentImageContext() {
|
|
UIGraphicsEndImageContext()
|
|
return image
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
|
|
// MARK: - Constraints
|
|
|
|
@objc public func addMaxConstraints(view: UIView) {
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
|
|
view.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
|
|
if #available(iOS 11.0, *) {
|
|
view.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor).isActive = true
|
|
} else {
|
|
view.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
|
|
}
|
|
if #available(iOS 11.0, *) {
|
|
view.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor).isActive = true
|
|
} else {
|
|
view.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
|
|
}
|
|
}
|
|
|
|
@objc public func addCenterConstraints(view: UIView) {
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
|
|
view.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
|
|
view.heightAnchor.constraint(equalToConstant: view.frame.size.height).isActive = true
|
|
view.widthAnchor.constraint(equalToConstant: view.frame.size.width).isActive = true
|
|
}
|
|
|
|
@objc public func addTopStickyConstraintsWithHeight(_ height: CGFloat, view: UIView) {
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
|
|
view.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
|
|
if #available(iOS 11.0, *) {
|
|
view.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor).isActive = true
|
|
} else {
|
|
view.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
|
|
}
|
|
view.heightAnchor.constraint(equalToConstant: height).isActive = true
|
|
}
|
|
|
|
@objc public func addBottomStickyConstraintsWithHeight(_ height: CGFloat, view: UIView) {
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
|
|
view.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
|
|
if #available(iOS 11.0, *) {
|
|
view.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor).isActive = true
|
|
} else {
|
|
view.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
|
|
}
|
|
view.heightAnchor.constraint(equalToConstant: height).isActive = true
|
|
}
|
|
|
|
@objc public func addMaxConstraintsToTopView(_ topView: UIView, forView view: UIView) {
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
view.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
|
|
view.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
|
|
view.topAnchor.constraint(equalTo: topView.bottomAnchor).isActive = true
|
|
if #available(iOS 11.0, *) {
|
|
view.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor).isActive = true
|
|
} else {
|
|
view.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
|
|
}
|
|
}
|
|
|
|
}
|
|
|