Skip to content

Instantly share code, notes, and snippets.

@yar1vn
Last active October 31, 2019 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yar1vn/a64df0e7ecaf2ba4a663 to your computer and use it in GitHub Desktop.
Save yar1vn/a64df0e7ecaf2ba4a663 to your computer and use it in GitHub Desktop.
UIButton for UIToolbar
//
// Button_.swift
//
// Created by Yariv on 1/23/15.
// Copyright (c) 2015 Yariv Nissim. All rights reserved.
//
import UIKit
class UIButton_: UIButton {
override func tintColorDidChange() {
if let tintColor = tintColor {
let normalImage = imageForState(.Normal)?.imageWithTint(tintColor, alpha: 0.3)
let selectedImage = imageForState(.Selected)?.imageWithTint(tintColor, alpha: 0.3)
// Make the button look like a UIBarButtonItem when highlighted
setImage(normalImage, forState: .Normal | .Highlighted)
setImage(selectedImage, forState: .Selected | .Highlighted)
}
}
}
private extension UIImage {
func imageWithTint(color: UIColor, alpha: CGFloat) -> UIImage {
let image = self.imageWithRenderingMode(.AlwaysTemplate)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIBezierPath(rect: CGRect(origin: CGPointZero, size: size)).fill()
image.drawAtPoint(CGPointZero, blendMode: kCGBlendModeDestinationIn, alpha: alpha)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment