SettingsViewController.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 {
  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. return UITableView.automaticDimension
  84. }
  85. override func numberOfSections(in tableView: UITableView) -> Int {
  86. return sectionTitles.count
  87. }
  88. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  89. switch section {
  90. case 0:
  91. if IQKeyboardManager.shared.enable == true {
  92. let properties = keyboardManagerProperties[section]
  93. return properties.count
  94. } else {
  95. return 1
  96. }
  97. case 1:
  98. if IQKeyboardManager.shared.enableAutoToolbar == false {
  99. return 1
  100. } else if IQKeyboardManager.shared.shouldShowToolbarPlaceholder == false {
  101. return 4
  102. } else {
  103. let properties = keyboardManagerProperties[section]
  104. return properties.count
  105. }
  106. case 2:
  107. if IQKeyboardManager.shared.overrideKeyboardAppearance == true {
  108. let properties = keyboardManagerProperties[section]
  109. return properties.count
  110. } else {
  111. return 1
  112. }
  113. case 3, 4, 5:
  114. let properties = keyboardManagerProperties[section]
  115. return properties.count
  116. default:
  117. return 0
  118. }
  119. }
  120. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  121. return sectionTitles[section]
  122. }
  123. override func tableView(_ tableView: UITableView,
  124. cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  125. switch indexPath.section {
  126. case 0:
  127. switch indexPath.row {
  128. case 0:
  129. guard let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell", for: indexPath) as? SwitchTableViewCell else {
  130. fatalError("Can't dequeue cell")
  131. }
  132. cell.switchEnable.isEnabled = true
  133. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  134. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  135. cell.switchEnable.isOn = IQKeyboardManager.shared.enable
  136. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  137. cell.switchEnable.addTarget(self, action: #selector(self.enableAction(_:)), for: .valueChanged)
  138. return cell
  139. case 1:
  140. guard let cell = tableView.dequeueReusableCell(withIdentifier: "StepperTableViewCell", for: indexPath) as? StepperTableViewCell else {
  141. fatalError("Can't dequeue cell")
  142. }
  143. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  144. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.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: break
  151. }
  152. case 1:
  153. switch indexPath.row {
  154. case 0:
  155. guard let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell", for: indexPath) as? SwitchTableViewCell else {
  156. fatalError("Can't dequeue cell")
  157. }
  158. cell.switchEnable.isEnabled = true
  159. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  160. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  161. cell.switchEnable.isOn = IQKeyboardManager.shared.enableAutoToolbar
  162. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  163. cell.switchEnable.addTarget(self, action: #selector(self.enableAutoToolbarAction(_:)), for: .valueChanged)
  164. return cell
  165. case 1:
  166. guard let cell = tableView.dequeueReusableCell(withIdentifier: "NavigationTableViewCell", for: indexPath) as? NavigationTableViewCell else {
  167. fatalError("Can't dequeue cell")
  168. }
  169. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  170. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  171. return cell
  172. case 2:
  173. guard let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell", for: indexPath) as? SwitchTableViewCell else {
  174. fatalError("Can't dequeue cell")
  175. }
  176. cell.switchEnable.isEnabled = true
  177. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  178. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  179. cell.switchEnable.isOn = IQKeyboardManager.shared.shouldToolbarUsesTextFieldTintColor
  180. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  181. cell.switchEnable.addTarget(self, action: #selector(self.shouldToolbarUsesTextFieldTintColorAction(_:)), for: .valueChanged)
  182. return cell
  183. case 3:
  184. guard let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell", for: indexPath) as? SwitchTableViewCell else {
  185. fatalError("Can't dequeue cell")
  186. }
  187. cell.switchEnable.isEnabled = true
  188. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  189. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  190. cell.switchEnable.isOn = IQKeyboardManager.shared.shouldShowToolbarPlaceholder
  191. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  192. cell.switchEnable.addTarget(self, action: #selector(self.shouldShowToolbarPlaceholder(_:)), for: .valueChanged)
  193. return cell
  194. case 4:
  195. guard let cell = tableView.dequeueReusableCell(withIdentifier: "NavigationTableViewCell", for: indexPath) as? NavigationTableViewCell else {
  196. fatalError("Can't dequeue cell")
  197. }
  198. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  199. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  200. return cell
  201. case 5:
  202. guard let cell = tableView.dequeueReusableCell(withIdentifier: "ColorTableViewCell", for: indexPath) as? ColorTableViewCell else {
  203. fatalError("Can't dequeue cell")
  204. }
  205. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  206. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  207. cell.colorPickerTextField.selectedColor = IQKeyboardManager.shared.toolbarTintColor
  208. cell.colorPickerTextField.tag = 15
  209. cell.colorPickerTextField.delegate = self
  210. return cell
  211. case 6:
  212. guard let cell = tableView.dequeueReusableCell(withIdentifier: "ImageSwitchTableViewCell", for: indexPath) as? ImageSwitchTableViewCell else {
  213. fatalError("Can't dequeue cell")
  214. }
  215. cell.switchEnable.isEnabled = true
  216. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  217. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  218. cell.arrowImageView.image = IQKeyboardManager.shared.toolbarDoneBarButtonItemImage
  219. cell.switchEnable.isOn = IQKeyboardManager.shared.toolbarDoneBarButtonItemImage != nil
  220. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  221. cell.switchEnable.addTarget(self, action: #selector(self.toolbarDoneBarButtonItemImage(_:)), for: .valueChanged)
  222. return cell
  223. case 7:
  224. guard let cell = tableView.dequeueReusableCell(withIdentifier: "TextFieldTableViewCell", for: indexPath) as? TextFieldTableViewCell else {
  225. fatalError("Can't dequeue cell")
  226. }
  227. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  228. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  229. cell.textField.text = IQKeyboardManager.shared.toolbarDoneBarButtonItemText
  230. cell.textField.tag = 17
  231. cell.textField.delegate = self
  232. return cell
  233. default: break
  234. }
  235. case 2:
  236. switch indexPath.row {
  237. case 0:
  238. guard let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell", for: indexPath) as? SwitchTableViewCell else {
  239. fatalError("Can't dequeue cell")
  240. }
  241. cell.switchEnable.isEnabled = true
  242. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  243. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  244. cell.switchEnable.isOn = IQKeyboardManager.shared.overrideKeyboardAppearance
  245. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  246. cell.switchEnable.addTarget(self, action: #selector(self.overrideKeyboardAppearanceAction(_:)), for: .valueChanged)
  247. return cell
  248. case 1:
  249. guard let cell = tableView.dequeueReusableCell(withIdentifier: "NavigationTableViewCell", for: indexPath) as? NavigationTableViewCell else {
  250. fatalError("Can't dequeue cell")
  251. }
  252. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  253. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  254. return cell
  255. default: break
  256. }
  257. case 3:
  258. switch indexPath.row {
  259. case 0:
  260. guard let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell", for: indexPath) as? SwitchTableViewCell else {
  261. fatalError("Can't dequeue cell")
  262. }
  263. cell.switchEnable.isEnabled = true
  264. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  265. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  266. cell.switchEnable.isOn = IQKeyboardManager.shared.shouldResignOnTouchOutside
  267. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  268. cell.switchEnable.addTarget(self, action: #selector(self.shouldResignOnTouchOutsideAction(_:)), for: .valueChanged)
  269. return cell
  270. default: break
  271. }
  272. case 4:
  273. switch indexPath.row {
  274. case 0:
  275. guard let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell", for: indexPath) as? SwitchTableViewCell else {
  276. fatalError("Can't dequeue cell")
  277. }
  278. cell.switchEnable.isEnabled = true
  279. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  280. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  281. cell.switchEnable.isOn = IQKeyboardManager.shared.shouldPlayInputClicks
  282. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  283. cell.switchEnable.addTarget(self, action: #selector(self.shouldPlayInputClicksAction(_:)), for: .valueChanged)
  284. return cell
  285. default: break
  286. }
  287. case 5:
  288. switch indexPath.row {
  289. case 0:
  290. guard let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchTableViewCell", for: indexPath) as? SwitchTableViewCell else {
  291. fatalError("Can't dequeue cell")
  292. }
  293. cell.switchEnable.isEnabled = true
  294. cell.labelTitle.text = keyboardManagerProperties[indexPath.section][indexPath.row]
  295. cell.labelSubtitle.text = keyboardManagerPropertyDetails[indexPath.section][indexPath.row]
  296. cell.switchEnable.isOn = IQKeyboardManager.shared.enableDebugging
  297. cell.switchEnable.removeTarget(nil, action: nil, for: .allEvents)
  298. cell.switchEnable.addTarget(self, action: #selector(self.enableDebugging(_:)), for: .valueChanged)
  299. return cell
  300. default: break
  301. }
  302. default: break
  303. }
  304. return UITableViewCell()
  305. }
  306. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  307. guard let identifier = segue.identifier else {
  308. return
  309. }
  310. if identifier.elementsEqual("OptionsViewController"), let controller = segue.destination as? OptionsViewController, let cell = sender as? UITableViewCell {
  311. controller.delegate = self
  312. selectedIndexPathForOptions = self.tableView.indexPath(for: cell)
  313. guard let selectedIndexPath = selectedIndexPathForOptions else {
  314. return
  315. }
  316. if selectedIndexPath.section == 1 && selectedIndexPath.row == 1 {
  317. controller.title = "Toolbar Manage Behaviour"
  318. controller.options = ["IQAutoToolbar By Subviews", "IQAutoToolbar By Tag", "IQAutoToolbar By Position"]
  319. controller.selectedIndex = IQKeyboardManager.shared.toolbarManageBehaviour.hashValue
  320. } else if selectedIndexPath.section == 1 && selectedIndexPath.row == 4 {
  321. controller.title = "Fonts"
  322. controller.options = ["Bold System Font", "Italic system font", "Regular"]
  323. controller.selectedIndex = IQKeyboardManager.shared.toolbarManageBehaviour.hashValue
  324. let fonts = [UIFont.boldSystemFont(ofSize: 12), UIFont.italicSystemFont(ofSize: 12), UIFont.systemFont(ofSize: 12)]
  325. if let placeholderFont = IQKeyboardManager.shared.placeholderFont {
  326. if let index = fonts.firstIndex(of: placeholderFont) {
  327. controller.selectedIndex = index
  328. }
  329. }
  330. } else if selectedIndexPath.section == 2 && selectedIndexPath.row == 1 {
  331. controller.title = "Keyboard Appearance"
  332. controller.options = ["UIKeyboardAppearance Default", "UIKeyboardAppearance Dark", "UIKeyboardAppearance Light"]
  333. controller.selectedIndex = IQKeyboardManager.shared.keyboardAppearance.hashValue
  334. }
  335. }
  336. }
  337. }
  338. extension SettingsViewController: ColorPickerTextFieldDelegate {
  339. func colorPickerTextField(_ textField: ColorPickerTextField, selectedColorAttributes colorAttributes: [String: Any] = [:]) {
  340. if textField.tag == 15, let color = colorAttributes["color"] as? UIColor {
  341. if color.isEqual(UIColor.clear) {
  342. IQKeyboardManager.shared.toolbarTintColor = nil
  343. } else {
  344. IQKeyboardManager.shared.toolbarTintColor = color
  345. }
  346. }
  347. }
  348. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  349. tableView.deselectRow(at: indexPath, animated: true)
  350. }
  351. func textFieldDidEndEditing(_ textField: UITextField) {
  352. if textField.tag == 17 {
  353. IQKeyboardManager.shared.toolbarDoneBarButtonItemText = textField.text?.isEmpty == false ? textField.text : nil
  354. }
  355. }
  356. }
  357. extension SettingsViewController: OptionsViewControllerDelegate {
  358. func optionsViewController(_ controller: OptionsViewController, index: NSInteger) {
  359. guard let selectedIndexPath = selectedIndexPathForOptions else {
  360. return
  361. }
  362. if selectedIndexPath.section == 1 && selectedIndexPath.row == 1 {
  363. IQKeyboardManager.shared.toolbarManageBehaviour = IQAutoToolbarManageBehaviour(rawValue: index)!
  364. } else if selectedIndexPath.section == 1 && selectedIndexPath.row == 4 {
  365. let fonts = [UIFont.boldSystemFont(ofSize: 12), UIFont.italicSystemFont(ofSize: 12), UIFont.systemFont(ofSize: 12)]
  366. IQKeyboardManager.shared.placeholderFont = fonts[index]
  367. } else if selectedIndexPath.section == 2 && selectedIndexPath.row == 1 {
  368. IQKeyboardManager.shared.keyboardAppearance = UIKeyboardAppearance(rawValue: index)!
  369. }
  370. }
  371. }