Skip to content

Instantly share code, notes, and snippets.

@nbasham
Created July 31, 2017 20:01
Show Gist options
  • Save nbasham/bfff542dfbd7281c455336386c0ec9b8 to your computer and use it in GitHub Desktop.
Save nbasham/bfff542dfbd7281c455336386c0ec9b8 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
@nbasham
Copy link
Author

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