Skip to content

poornaonline/UIViewControllerExtensionsV1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

UIViewController Extensions

This Swift extension contains useful extension methods for UIViewController

Hide Keyboard when tap around

func hideKeyboardWhenTappedAround() {
        
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action:                                                  #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }
    
    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
}

Show Basic Alert using extension

func showBasicAlert(title: String, message: String) {
        
        let alert = UIAlertController(title: title, message: message, preferredStyle:                                                               UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Okay", 
                                     style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
        
}

Show Basic Alert and go back in Navigation View Controller (to Previous View Controller)

func showBasicAlertAndGoBackInNVC(title: String, message: String) {
        
        let alert = UIAlertController(title: title, message: message, preferredStyle:                                             UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, 
        handler: {[weak self] (alert) in
            guard let strongSelf = self else { return }
            strongSelf.navigationController?.popViewController(animated: true)
        }))
        self.present(alert, animated: true, completion: nil)
}

Show Basic Alert and dismiss the Navigation View Controller

func showBasicAlertWithDismissNVC(title: String, message: String) {
        
        let alert = UIAlertController(title: title, message: message, preferredStyle:                                             UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Okay", style: UIAlertActionStyle.default, 
        handler: {[weak self] (alert) in
            guard let strongSelf = self else { return }
            strongSelf.navigationController?.dismiss(animated: true, completion: nil)
        }))
        self.present(alert, animated: true, completion: nil)
}