SettingsViewController.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //
  2. // SettingsViewController.swift
  3. // Demo
  4. //
  5. // Created by Iftekhar on 26/08/15.
  6. // Copyright (c) 2015 Iftekhar. All rights reserved.
  7. //
  8. import UIKit
  9. import IQKeyboardManagerSwift
  10. class SettingsViewController: UITableViewController, OptionsViewControllerDelegate, ColorPickerTextFieldDelegate {
  11. let sectionTitles = ["UIKeyboard handling",
  12. "IQToolbar handling",
  13. "UIKeyboard appearance overriding",
  14. "Resign first responder handling",
  15. "UISound handling",
  16. "IQKeyboardManager Debug"]
  17. let keyboardManagerProperties = [["Enable", "Keyboard Distance From TextField"],
  18. ["Enable AutoToolbar","Toolbar Manage Behaviour","Should Toolbar Uses TextField TintColor","Should Show TextField Placeholder","Placeholder Font","Toolbar Tint Color","Toolbar Done BarButtonItem Image","Toolbar Done Button Text"],
  19. ["Override Keyboard Appearance","UIKeyboard Appearance"],
  20. ["Should Resign On Touch Outside"],
  21. ["Should Play Input Clicks"],
  22. ["Debugging logs in Console"]]
  23. let keyboardManagerPropertyDetails = [["Enable/Disable IQKeyboardManager","Set keyboard distance from textField"],
  24. ["Automatic add the IQToolbar on UIKeyboard","AutoToolbar previous/next button managing behaviour","Uses textField's tintColor property for IQToolbar","Add the textField's placeholder text on IQToolbar","UIFont for IQToolbar placeholder text","Override toolbar tintColor property","Replace toolbar done button text with provided image","Override toolbar done button text"],
  25. ["Override the keyboardAppearance for all UITextField/UITextView","All the UITextField keyboardAppearance is set using this property"],
  26. ["Resigns Keyboard on touching outside of UITextField/View"],
  27. ["Plays inputClick sound on next/previous/done click"],
  28. ["Setting enableDebugging to YES/No to turn on/off debugging mode"]]
  29. var selectedIndexPathForOptions : IndexPath?
  30. @IBAction func doneAction (_ sender: UIBarButtonItem) {
  31. self.dismiss(animated: true, completion: nil)
  32. }
  33. /** UIKeyboard Handling */
  34. @objc func enableAction (_ sender: UISwitch) {
  35. IQKeyboardManager.shared.enable = sender.isOn
  36. self.tableView.reloadSections(IndexSet(integer: 0), with: .fade)
  37. }
  38. @objc func keyboardDistanceFromTextFieldAction (_ sender: UIStepper) {
  39. IQKeyboardManager.shared.keyboardDistanceFromTextField = CGFloat(sender.value)
  40. self.tableView.reloadRows(at: [IndexPath(row: 1, section: 0)], with: .none)
  41. }
  42. /** IQToolbar handling */
  43. @objc func enableAutoToolbarAction (_ sender: UISwitch) {
  44. IQKeyboardManager.shared.enableAutoToolbar = sender.isOn
  45. self.tableView.reloadSections(IndexSet(integer: 1), with: .fade)
  46. }
  47. @objc func shouldToolbarUsesTextFieldTintColorAction (_ sender: UISwitch) {
  48. IQKeyboardManager.shared.shouldToolbarUsesTextFieldTintColor = sender.isOn
  49. }
  50. @objc func shouldShowToolbarPlaceholder (_ sender: UISwitch) {
  51. IQKeyboardManager.shared.shouldShowToolbarPlaceholder = sender.isOn
  52. self.tableView.reloadSections(IndexSet(integer: 1), with: .fade)
  53. }
  54. @objc func toolbarDoneBarButtonItemImage (_ sender: UISwitch) {
  55. if sender.isOn {
  56. IQKeyboardManager.shared.toolbarDoneBarButtonItemImage = UIImage(named:"IQButtonBarArrowDown")
  57. } else {
  58. IQKeyboardManager.shared.toolbarDoneBarButtonItemImage = nil
  59. }
  60. self.tableView.reloadSections(IndexSet(integer: 1), with: .fade)
  61. }
  62. /** "Keyboard appearance overriding */
  63. @objc func overrideKeyboardAppearanceAction (_ sender: UISwitch) {
  64. IQKeyboardManager.shared.overrideKeyboardAppearance = sender.isOn
  65. self.tableView.reloadSections(IndexSet(integer: 2), with: .fade)
  66. }
  67. /** Resign first responder handling */
  68. @objc func shouldResignOnTouchOutsideAction (_ sender: UISwitch) {
  69. IQKeyboardManager.shared.shouldResignOnTouchOutside = sender.isOn
  70. }
  71. /** Sound handling */
  72. @objc func shouldPlayInputClicksAction (_ sender: UISwitch) {
  73. IQKeyboardManager.shared.shouldPlayInputClicks = sender.isOn
  74. }
  75. /** Debugging */
  76. @objc func enableDebugging (_ sender: UISwitch) {
  77. IQKeyboardManager.shared.enableDebugging = sender.isOn
  78. }
  79. override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  80. return 80
  81. }
  82. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  83. #if swift(>=4.2)
  84. return UITableView.automaticDimension
  85. #else
  86. return UITableViewAutomaticDimension
  87. #endif
  88. }
  89. override func numberOfSections(in tableView: UITableView) -> Int {
  90. return sectionTitles.count
  91. }
  92. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  93. switch (section)
  94. {
  95. case 0:
  96. if IQKeyboardManager.shared.enable == true {
  97. let properties = keyboardManagerProperties[section]
  98. return properties.count
  99. } else {
  100. return 1
  101. }
  102. case 1:
  103. if IQKeyboardManager.shared.enableAutoToolbar == false {
  104. return 1
  105. } else if IQKeyboardManager.shared.shouldShowToolbarPlaceholder == false {
  106. return 4
  107. } else {
  108. let properties = keyboardManagerProperties[section]
  109. return properties.count
  110. }
  111. case 2:
  112. if IQKeyboardManager.shared.overrideKeyboardAppearance == true {
  113. let properties = keyboardManagerProperties[section]
  114. return properties.count
  115. } else {
  116. return 1
  117. }
  118. case 3,4,5:
  119. let properties = keyboardManagerProperties[section]
  120. return properties.count
  121. default:
  122. return 0
  123. }
  124. }
  125. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  126. return sectionTitles[section]
  127. }
  128. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  129. switch ((indexPath as NSIndexPath).section) {
  130. case 0:
  131. switch ((indexPath as NSIndexPath).row) {
  132. case 0:
  133. let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell") as! SwitchTableViewCell
  134. cell.switchEnable.isEnabled = true
  135. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  136. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  137. cell.switchEnable.isOn = IQKeyboardManager.shared.enable
  138. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  139. cell.switchEnable.addTarget(self, action: #selector(self.enableAction(_:)), for: .valueChanged)
  140. return cell
  141. case 1:
  142. let cell = tableView.dequeueReusableCell(withIdentifier: "StepperTableViewCell") as! StepperTableViewCell
  143. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  144. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  145. cell.stepper.value = Double(IQKeyboardManager.shared.keyboardDistanceFromTextField)
  146. cell.labelStepperValue.text = NSString(format: "%.0f", IQKeyboardManager.shared.keyboardDistanceFromTextField) as String
  147. cell.stepper.removeTarget(nil, action: nil, for: .allEvents)
  148. cell.stepper.addTarget(self, action: #selector(self.keyboardDistanceFromTextFieldAction(_:)), for: .valueChanged)
  149. return cell
  150. default:
  151. break
  152. }
  153. case 1:
  154. switch ((indexPath as NSIndexPath).row) {
  155. case 0:
  156. let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell") as! SwitchTableViewCell
  157. cell.switchEnable.isEnabled = true
  158. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  159. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  160. cell.switchEnable.isOn = IQKeyboardManager.shared.enableAutoToolbar
  161. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  162. cell.switchEnable.addTarget(self, action: #selector(self.enableAutoToolbarAction(_:)), for: .valueChanged)
  163. return cell
  164. case 1:
  165. let cell = tableView.dequeueReusableCell(withIdentifier: "NavigationTableViewCell") as! NavigationTableViewCell
  166. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  167. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  168. return cell
  169. case 2:
  170. let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell") as! SwitchTableViewCell
  171. cell.switchEnable.isEnabled = true
  172. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  173. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  174. cell.switchEnable.isOn = IQKeyboardManager.shared.shouldToolbarUsesTextFieldTintColor
  175. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  176. cell.switchEnable.addTarget(self, action: #selector(self.shouldToolbarUsesTextFieldTintColorAction(_:)), for: .valueChanged)
  177. return cell
  178. case 3:
  179. let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell") as! SwitchTableViewCell
  180. cell.switchEnable.isEnabled = true
  181. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  182. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  183. cell.switchEnable.isOn = IQKeyboardManager.shared.shouldShowToolbarPlaceholder
  184. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  185. cell.switchEnable.addTarget(self, action: #selector(self.shouldShowToolbarPlaceholder(_:)), for: .valueChanged)
  186. return cell
  187. case 4:
  188. let cell = tableView.dequeueReusableCell(withIdentifier: "NavigationTableViewCell") as! NavigationTableViewCell
  189. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  190. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  191. return cell
  192. case 5:
  193. let cell = tableView.dequeueReusableCell(withIdentifier: "ColorTableViewCell") as! ColorTableViewCell
  194. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  195. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  196. cell.colorPickerTextField.selectedColor = IQKeyboardManager.shared.toolbarTintColor
  197. cell.colorPickerTextField.tag = 15
  198. cell.colorPickerTextField.delegate = self
  199. return cell
  200. case 6:
  201. let cell = tableView.dequeueReusableCell(withIdentifier: "ImageSwitchTableViewCell") as! ImageSwitchTableViewCell
  202. cell.switchEnable.isEnabled = true
  203. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  204. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  205. cell.arrowImageView.image = IQKeyboardManager.shared.toolbarDoneBarButtonItemImage
  206. cell.switchEnable.isOn = IQKeyboardManager.shared.toolbarDoneBarButtonItemImage != nil
  207. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  208. cell.switchEnable.addTarget(self, action: #selector(self.toolbarDoneBarButtonItemImage(_:)), for: .valueChanged)
  209. return cell
  210. case 7:
  211. let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldTableViewCell") as! TextFieldTableViewCell
  212. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  213. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  214. cell.textField.text = IQKeyboardManager.shared.toolbarDoneBarButtonItemText
  215. cell.textField.tag = 17
  216. cell.textField.delegate = self
  217. return cell
  218. default:
  219. break
  220. }
  221. case 2:
  222. switch ((indexPath as NSIndexPath).row) {
  223. case 0:
  224. let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell") as! SwitchTableViewCell
  225. cell.switchEnable.isEnabled = true
  226. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  227. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  228. cell.switchEnable.isOn = IQKeyboardManager.shared.overrideKeyboardAppearance
  229. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  230. cell.switchEnable.addTarget(self, action: #selector(self.overrideKeyboardAppearanceAction(_:)), for: .valueChanged)
  231. return cell
  232. case 1:
  233. let cell = tableView.dequeueReusableCell(withIdentifier: "NavigationTableViewCell") as! NavigationTableViewCell
  234. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  235. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  236. return cell
  237. default:
  238. break
  239. }
  240. case 3:
  241. switch ((indexPath as NSIndexPath).row) {
  242. case 0:
  243. let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell") as! SwitchTableViewCell
  244. cell.switchEnable.isEnabled = true
  245. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  246. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  247. cell.switchEnable.isOn = IQKeyboardManager.shared.shouldResignOnTouchOutside
  248. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  249. cell.switchEnable.addTarget(self, action: #selector(self.shouldResignOnTouchOutsideAction(_:)), for: .valueChanged)
  250. return cell
  251. default:
  252. break
  253. }
  254. case 4:
  255. switch ((indexPath as NSIndexPath).row) {
  256. case 0:
  257. let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell") as! SwitchTableViewCell
  258. cell.switchEnable.isEnabled = true
  259. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  260. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  261. cell.switchEnable.isOn = IQKeyboardManager.shared.shouldPlayInputClicks
  262. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  263. cell.switchEnable.addTarget(self, action: #selector(self.shouldPlayInputClicksAction(_:)), for: .valueChanged)
  264. return cell
  265. default:
  266. break
  267. }
  268. case 5:
  269. switch ((indexPath as NSIndexPath).row) {
  270. case 0:
  271. let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell") as! SwitchTableViewCell
  272. cell.switchEnable.isEnabled = true
  273. cell.labelTitle.text = keyboardManagerProperties[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  274. cell.labelSubtitle.text = keyboardManagerPropertyDetails[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row]
  275. cell.switchEnable.isOn = IQKeyboardManager.shared.enableDebugging
  276. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  277. cell.switchEnable.addTarget(self, action: #selector(self.enableDebugging(_:)), for: .valueChanged)
  278. return cell
  279. default:
  280. break
  281. }
  282. default:
  283. break
  284. }
  285. return UITableViewCell()
  286. }
  287. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  288. tableView.deselectRow(at: indexPath, animated: true)
  289. }
  290. func colorPickerTextField(_ textField: ColorPickerTextField, selectedColorAttributes colorAttributes: [String : Any] = [:]) {
  291. if textField.tag == 15 {
  292. let color = colorAttributes["color"] as! UIColor
  293. if color.isEqual(UIColor.clear) {
  294. IQKeyboardManager.shared.toolbarTintColor = nil
  295. } else {
  296. IQKeyboardManager.shared.toolbarTintColor = color
  297. }
  298. }
  299. }
  300. func textFieldDidEndEditing(_ textField: UITextField) {
  301. if textField.tag == 17 {
  302. IQKeyboardManager.shared.toolbarDoneBarButtonItemText = textField.text?.isEmpty == false ? textField.text : nil
  303. }
  304. }
  305. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  306. if let identifier = segue.identifier {
  307. if identifier == "OptionsViewController" {
  308. let controller = segue.destination as! OptionsViewController
  309. controller.delegate = self
  310. let cell = sender as! UITableViewCell
  311. selectedIndexPathForOptions = self.tableView.indexPath(for: cell)
  312. if let selectedIndexPath = selectedIndexPathForOptions {
  313. if (selectedIndexPath as NSIndexPath).section == 1 && (selectedIndexPath as NSIndexPath).row == 1 {
  314. controller.title = "Toolbar Manage Behaviour"
  315. controller.options = ["IQAutoToolbar By Subviews","IQAutoToolbar By Tag","IQAutoToolbar By Position"]
  316. controller.selectedIndex = IQKeyboardManager.shared.toolbarManageBehaviour.hashValue
  317. } else if (selectedIndexPath as NSIndexPath).section == 1 && (selectedIndexPath as NSIndexPath).row == 4 {
  318. controller.title = "Fonts"
  319. controller.options = ["Bold System Font","Italic system font","Regular"]
  320. controller.selectedIndex = IQKeyboardManager.shared.toolbarManageBehaviour.hashValue
  321. let fonts = [UIFont.boldSystemFont(ofSize: 12),UIFont.italicSystemFont(ofSize: 12),UIFont.systemFont(ofSize: 12)]
  322. if let placeholderFont = IQKeyboardManager.shared.placeholderFont {
  323. if let index = fonts.index(of: placeholderFont) {
  324. controller.selectedIndex = index
  325. }
  326. }
  327. } else if (selectedIndexPath as NSIndexPath).section == 2 && (selectedIndexPath as NSIndexPath).row == 1 {
  328. controller.title = "Keyboard Appearance"
  329. controller.options = ["UIKeyboardAppearance Default","UIKeyboardAppearance Dark","UIKeyboardAppearance Light"]
  330. controller.selectedIndex = IQKeyboardManager.shared.keyboardAppearance.hashValue
  331. }
  332. }
  333. }
  334. }
  335. }
  336. func optionsViewController(_ controller: OptionsViewController, index: NSInteger) {
  337. if let selectedIndexPath = selectedIndexPathForOptions {
  338. if (selectedIndexPath as NSIndexPath).section == 1 && (selectedIndexPath as NSIndexPath).row == 1 {
  339. IQKeyboardManager.shared.toolbarManageBehaviour = IQAutoToolbarManageBehaviour(rawValue: index)!
  340. } else if (selectedIndexPath as NSIndexPath).section == 1 && (selectedIndexPath as NSIndexPath).row == 4 {
  341. let fonts = [UIFont.boldSystemFont(ofSize: 12),UIFont.italicSystemFont(ofSize: 12),UIFont.systemFont(ofSize: 12)]
  342. IQKeyboardManager.shared.placeholderFont = fonts[index]
  343. } else if (selectedIndexPath as NSIndexPath).section == 2 && (selectedIndexPath as NSIndexPath).row == 1 {
  344. IQKeyboardManager.shared.keyboardAppearance = UIKeyboardAppearance(rawValue: index)!
  345. }
  346. }
  347. }
  348. }