Use this code for taking a screenshot using UIToolBar
extension UIViewController: UITextFieldDelegate{
func addToolBar(textField: UITextField){
var toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
var screenshotButton = UIBarButtonItem(title: "Screenshot", style: UIBarButtonItemStyle.Done, target: self, action: "donePressed")
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "cancelPressed")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true
toolBar.sizeToFit()
textField.delegate = self
textField.inputAccessoryView = toolBar
}
func screenshotButton(){
func captureScreen() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
func cancelPressed(){
view.endEditing(true) // or do something
}
}
Though I still need to call the code below on all of my text fields.
override func viewDidLoad() {
super.viewDidLoad()
addToolBar(addressField)
}