Skip to content

Instantly share code, notes, and snippets.

@msuzoagu
Forked from nbasham/CloseButton.playground
Created January 27, 2023 17:42
Show Gist options
  • Save msuzoagu/9d5560466f5e63ba16b0b40916be0f9d to your computer and use it in GitHub Desktop.
Save msuzoagu/9d5560466f5e63ba16b0b40916be0f9d to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment