Created
May 23, 2022 16:28
-
-
Save hishd/969360f0eb59cb6004dc6724581ca4c8 to your computer and use it in GitHub Desktop.
Settings UIKit Constraints
This file contains 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
// | |
// Extensions.swift | |
// | |
// Created by Hishara Dilshan on 2022-04-24. | |
// | |
import UIKit | |
extension UIView { | |
func setupToolbar() -> UIToolbar { | |
let toolbar = UIToolbar() | |
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) | |
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(doneButtonTapped)) | |
toolbar.setItems([flexSpace, doneButton], animated: true) | |
toolbar.sizeToFit() | |
return toolbar | |
} | |
@objc func doneButtonTapped() { | |
endEditing(true) | |
} | |
} | |
extension UIViewController { | |
func configureGradientLayer() { | |
let gradient = CAGradientLayer() | |
gradient.colors = [UIColor.systemPurple.cgColor, UIColor.systemBlue.cgColor] | |
gradient.locations = [0, 1] | |
view.layer.addSublayer(gradient) | |
gradient.frame = view.frame | |
} | |
func showMessage(withTitle: String, message: String) { | |
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "Ok", style: .default)) | |
present(alert, animated: true, completion: nil) | |
} | |
} | |
extension UIButton { | |
func attributedTitle(first: String, second: String, fontSize: CGFloat) { | |
let attrs : [NSAttributedString.Key: Any] = [.foregroundColor : UIColor(white: 1, alpha: 0.87), .font: UIFont.systemFont(ofSize: fontSize)] | |
let attributedTitle = NSMutableAttributedString(string: "\(first) ", attributes: attrs) | |
let boldAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor(white: 1, alpha: 0.87), .font: UIFont.boldSystemFont(ofSize: fontSize)] | |
attributedTitle.append(NSAttributedString(string: second, attributes: boldAttrs)) | |
setAttributedTitle(attributedTitle, for: .normal) | |
} | |
} | |
extension UIView { | |
func anchor(top: NSLayoutYAxisAnchor? = nil, | |
left: NSLayoutXAxisAnchor? = nil, | |
bottom: NSLayoutYAxisAnchor? = nil, | |
right: NSLayoutXAxisAnchor? = nil, | |
paddingTop: CGFloat = 0, | |
paddingLeft: CGFloat = 0, | |
paddingBottom: CGFloat = 0, | |
paddingRight: CGFloat = 0, | |
width: CGFloat? = nil, | |
height: CGFloat? = nil) { | |
translatesAutoresizingMaskIntoConstraints = false | |
if let top = top { | |
topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true | |
} | |
if let left = left { | |
leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true | |
} | |
if let bottom = bottom { | |
bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true | |
} | |
if let right = right { | |
rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true | |
} | |
if let width = width { | |
widthAnchor.constraint(equalToConstant: width).isActive = true | |
} | |
if let height = height { | |
heightAnchor.constraint(equalToConstant: height).isActive = true | |
} | |
} | |
func center(inView view: UIView, yConstant: CGFloat? = 0) { | |
translatesAutoresizingMaskIntoConstraints = false | |
centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: yConstant!).isActive = true | |
} | |
func centerX(inView view: UIView, topAnchor: NSLayoutYAxisAnchor? = nil, paddingTop: CGFloat? = 0) { | |
translatesAutoresizingMaskIntoConstraints = false | |
centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true | |
if let topAnchor = topAnchor { | |
self.topAnchor.constraint(equalTo: topAnchor, constant: paddingTop!).isActive = true | |
} | |
} | |
func centerY(inView view: UIView, leftAnchor: NSLayoutXAxisAnchor? = nil, paddingLeft: CGFloat = 0, constant: CGFloat = 0) { | |
translatesAutoresizingMaskIntoConstraints = false | |
centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: constant).isActive = true | |
if let leftAnchor = leftAnchor { | |
anchor(left: leftAnchor, paddingLeft: paddingLeft) | |
} | |
} | |
func setDimensions(height: CGFloat, width: CGFloat) { | |
translatesAutoresizingMaskIntoConstraints = false | |
heightAnchor.constraint(equalToConstant: height).isActive = true | |
widthAnchor.constraint(equalToConstant: width).isActive = true | |
} | |
func setHeight(of height: CGFloat) { | |
translatesAutoresizingMaskIntoConstraints = false | |
heightAnchor.constraint(equalToConstant: height).isActive = true | |
} | |
func setWidth(of width: CGFloat) { | |
translatesAutoresizingMaskIntoConstraints = false | |
widthAnchor.constraint(equalToConstant: width).isActive = true | |
} | |
func fillSuperView() { | |
translatesAutoresizingMaskIntoConstraints = false | |
guard let view = superview else { return } | |
anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment