import UIKit
class NewRestaurantController: UITableViewController ,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
@IBOutlet weak var photoImageView:UIImageView!
@IBOutlet weak var nameTextField: RoundedTextField!{
didSet{
nameTextField.tag = 1
nameTextField.becomeFirstResponder()
nameTextField.delegate = self
}
}
@IBOutlet weak var typeTextField: RoundedTextField!{
didSet{
typeTextField.tag = 2
typeTextField.delegate = self
}
}
@IBOutlet weak var addressTextField: RoundedTextField!{
didSet{
addressTextField.tag = 3
addressTextField.delegate = self
}
}
@IBOutlet weak var phoneTextField: RoundedTextField!{
didSet{
phoneTextField.tag = 4
phoneTextField.delegate = self
}
}
@IBOutlet weak var descriptionTextView: UITextView!{
didSet{
descriptionTextView.tag = 5
descriptionTextView.layer.cornerRadius = 5.0
descriptionTextView.layer.masksToBounds = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.tintColor = UIColor.white
navigationController?.navigationBar.shadowImage = UIImage()
if let customFont = UIFont(name: "Rubik-Medium", size: 35.0){
navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.brown,NSAttributedStringKey.font:customFont]
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if let nextTextField = view.viewWithTag(textField.tag + 1){
textField.resignFirstResponder()
nextTextField.becomeFirstResponder()
}
return true
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0{
let photoSourceRequestController = UIAlertController(title: "", message: "Choose our photo source", preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) { (action) in
if UIImagePickerController.isSourceTypeAvailable(.camera){
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
imagePicker.sourceType = .camera
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
}
}
let photoLibraryAction = UIAlertAction(title: "Photo Library", style: .default, handler: { (action) in
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
self.present(imagePicker, animated: true, completion: nil)
}
})
photoSourceRequestController.addAction(cameraAction)
photoSourceRequestController.addAction(photoLibraryAction)
self.present(photoSourceRequestController, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
photoImageView.image = selectedImage
photoImageView.contentMode = .scaleAspectFill
photoImageView.clipsToBounds = true
}
let leadingConstraint = NSLayoutConstraint(item: photoImageView, attribute: .leading, relatedBy: .equal, toItem: photoImageView.superview, attribute: .leading, multiplier: 1, constant: 0)
// 约束条件在实例化后不会启动,你必须设定它的isActive属性为true来启动它
leadingConstraint.isActive = true
let trailingConstraint = NSLayoutConstraint(item: photoImageView, attribute: .trailing, relatedBy: .equal, toItem: photoImageView.superview, attribute: .trailing, multiplier: 1, constant: 0)
trailingConstraint.isActive = true
let topConstraint = NSLayoutConstraint(item: photoImageView, attribute: .top, relatedBy: .equal, toItem: photoImageView.superview, attribute: .top, multiplier: 1, constant: 0)
topConstraint.isActive = true
let bottomConstraint = NSLayoutConstraint(item: photoImageView, attribute: .bottom, relatedBy: .equal, toItem: photoImageView.superview, attribute: .bottom, multiplier: 1, constant: 0)
bottomConstraint.isActive = true
dismiss(animated: true, completion: nil)
}
let leadingConstraint = NSLayoutConstraint(item: photoImageView, attribute: .leading, relatedBy: .equal, toItem: photoImageView.superview, attribute: .leading, multiplier: 1, constant: 0)
// 约束条件在实例化后不会启动,你必须设定它的isActive属性为true来启动它
leadingConstraint.isActive = true
let trailingConstraint = NSLayoutConstraint(item: photoImageView, attribute: .trailing, relatedBy: .equal, toItem: photoImageView.superview, attribute: .trailing, multiplier: 1, constant: 0)
trailingConstraint.isActive = true
let topConstraint = NSLayoutConstraint(item: photoImageView, attribute: .top, relatedBy: .equal, toItem: photoImageView.superview, attribute: .top, multiplier: 1, constant: 0)
topConstraint.isActive = true
let bottomConstraint = NSLayoutConstraint(item: photoImageView, attribute: .bottom, relatedBy: .equal, toItem: photoImageView.superview, attribute: .bottom, multiplier: 1, constant: 0)
bottomConstraint.isActive = true