備忘録
下記の動画より学んだ便利な関数です。
レイアウトやサイズの設定
anchor
extension UIView {
func anchor(
top: NSLayoutYAxisAnchor? = nil,
bottom: NSLayoutYAxisAnchor? = nil,
left: NSLayoutXAxisAnchor? = nil,
right: NSLayoutXAxisAnchor? = nil,
centerY: NSLayoutYAxisAnchor? = nil,
centerX: NSLayoutXAxisAnchor? = nil,
width: CGFloat? = nil,
height: CGFloat? = nil,
topPadding: CGFloat = 0,
bottomPadding: CGFloat = 0,
leftPadding: CGFloat = 0,
rightPadding: CGFloat = 0) {
self.translatesAutoresizingMaskIntoConstraints = false
if let top = top {
self.topAnchor.constraint(equalTo: top, constant: topPadding).isActive = true
}
if let bottom = bottom {
self.bottomAnchor.constraint(equalTo: bottom, constant: -bottomPadding).isActive = true
}
if let left = left {
self.leftAnchor.constraint(equalTo: left, constant: leftPadding).isActive = true
}
if let right = right {
self.rightAnchor.constraint(equalTo: right, constant: -rightPadding).isActive = true
}
if let centerY = centerY {
self.centerYAnchor.constraint(equalTo: centerY).isActive = true
}
if let centerX = centerX {
self.centerXAnchor.constraint(equalTo: centerX).isActive = true
}
if let width = width {
self.widthAnchor.constraint(equalToConstant: width).isActive = true
}
if let height = height {
self.heightAnchor.constraint(equalToConstant: height).isActive = true
}
}
}
画像のリサイズ
resize
extension UIImage {
func resize(size _size: CGSize) -> UIImage? {
let widthRatio = _size.width / size.width
let heightRatio = _size.height / size.height
let ratio = widthRatio < heightRatio ? widthRatio : heightRatio
let resizeSize = CGSize(width: size.width * ratio, height: size.height * ratio)
UIGraphicsBeginImageContextWithOptions(resizeSize,false, 0.0)
draw(in: CGRect(origin: .zero, size: resizeSize))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resizedImage
}
}
色の設定
rgb
extension UIColor {
static func rgb(red:CGFloat,green:CGFloat,blue:CGFloat) -> UIColor{
return .init(red: red/255, green: green/255, blue: blue/255, alpha: 1)
}
}