Skip to content

Instantly share code, notes, and snippets.

@gonzoooooo
Created November 6, 2019 00:23
Show Gist options
  • Save gonzoooooo/0db492b259b910303cc03cc986769e72 to your computer and use it in GitHub Desktop.
Save gonzoooooo/0db492b259b910303cc03cc986769e72 to your computer and use it in GitHub Desktop.
UIBarButtonItem のアイコンをリサイズする
import UIKit
extension UIImage {
func scale(to newSize: CGSize) -> UIImage {
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
draw(in: CGRect(x: 0.0, y: 0.0, width: newSize.width, height: newSize.height))
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage ?? self
}
}
@IBDesignable
class ResizingImageBarButtonItem: UIBarButtonItem {
@IBInspectable
var scaledRatio: CGFloat = 1.0 {
didSet {
guard let size = image?.size.applying(CGAffineTransform(scaleX: scaledRatio, y: scaledRatio)) else { return }
image = image?.scale(to: size)
}
}
@IBInspectable
var scaledWidth: CGFloat = 0.0 {
didSet {
image = image?.scale(to: CGSize(width: scaledWidth, height: scaledHeight))
}
}
@IBInspectable
var scaledHeight: CGFloat = 0.0 {
didSet {
image = image?.scale(to: CGSize(width: scaledWidth, height: scaledHeight))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment