EZSwiftExtensions/Sources/UIButtonExtensions.swift

32 lines
1.1 KiB
Swift

//
// UIButtonExtensions.swift
// EZSwiftExtensions
//
// Created by Goktug Yilmaz on 15/07/15.
// Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
//
import UIKit
extension UIButton {
/// EZSwiftExtensions
// swiftlint:disable function_parameter_count
public convenience init(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat, target: AnyObject, action: Selector) {
self.init(frame: CGRect(x: x, y: y, width: w, height: h))
addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
}
// swiftlint:enable function_parameter_count
/// EZSwiftExtensions
public func setBackgroundColor(color: UIColor, forState: UIControlState) {
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
guard let context = UIGraphicsGetCurrentContext() else { return }
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, CGRect(x: 0, y: 0, width: 1, height: 1))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.setBackgroundImage(colorImage, forState: forState)
}
}