Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
UIButton close X Swift
import UIKit
import PlaygroundSupport
func getCloseButton(frame: CGRect, color: UIColor) -> UIButton? {
guard frame.size.width == frame.size.height else { return nil }
let button = UIButton(type: .custom)
button.frame = frame
button.setTitleColor(color, for: .normal)
button.setTitle("X", for: .normal)
button.layer.borderColor = color.cgColor
button.layer.borderWidth = 1
button.layer.cornerRadius = frame.size.height / 2.0
return button
}
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
containerView.backgroundColor = UIColor.blue
let buttonFrame = CGRect(x: 20, y: 20, width: 36, height: 36)
if let button = getCloseButton(frame: buttonFrame, color: .white) {
containerView.addSubview(button)
}
PlaygroundPage.current.liveView = containerView
@nbasham

This comment has been minimized.

Copy link
Owner Author

@nbasham nbasham commented Jul 31, 2017

Example
closexbutton

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment