[关闭]
@buoge 2017-07-27T16:07:36.000000Z 字数 1879 阅读 1606

InputAccessoryView 键盘上方自定义Action按钮: 上一步,下一步,完成

iOS


99DB9804-EED3-42EC-B9D0-EE09C681B981.png-47.8kB

1. ViewController 实现 ZZInputAccessoryViewDelegate

2. 添加ZZInputAccessoryView到UITextField

  1. let accessoryView = ZZInputAccessoryView.genZZInputAccessoryView()
  2. accessoryView.viewDelegate = self
  3. textField.inputAccessoryView = accessoryView

具体实现

  1. //
  2. // ZZInputAccessoryView.swift
  3. // zaozuo-ios
  4. //
  5. // Created by wuchuanbo on 2017/7/26.
  6. // Copyright © 2017年 zaozuo. All rights reserved.
  7. //
  8. import Foundation
  9. public protocol ZZInputAccessoryViewDelegate: NSObjectProtocol {
  10. func actionDone()
  11. func actionPrev()
  12. func actionNext()
  13. }
  14. class ZZInputAccessoryView: UIToolbar {
  15. private var prevButton: UIBarButtonItem?
  16. private var nextButton: UIBarButtonItem?
  17. private var doneButton: UIBarButtonItem?
  18. weak var viewDelegate: ZZInputAccessoryViewDelegate?
  19. static func genZZInputAccessoryView() -> ZZInputAccessoryView {
  20. let viewFrame = CGRect(x: 0, y: 0, width: AppWidth, height: 35)
  21. return ZZInputAccessoryView(frame: viewFrame)
  22. }
  23. override init(frame: CGRect) {
  24. super.init(frame: frame)
  25. //toolbar.tintColor = UIColor.blue;
  26. self.backgroundColor = UIColor.gray;
  27. prevButton = UIBarButtonItem(title: "上一步", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ZZInputAccessoryView.actionPrev))
  28. nextButton = UIBarButtonItem(title: "下一步", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ZZInputAccessoryView.actionNext))
  29. let space: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
  30. doneButton = UIBarButtonItem(title: "完成", style: UIBarButtonItemStyle.plain, target: self, action: #selector(ZZInputAccessoryView.actionDone))
  31. if let _prevButton = prevButton,
  32. let _nextButton = nextButton , let _doneButton = doneButton {
  33. self.items = [_prevButton, _nextButton, space, _doneButton]
  34. }
  35. }
  36. required init?(coder aDecoder: NSCoder) {
  37. super.init(coder: aDecoder)
  38. zzerror("init(coder:) has not been implemented")
  39. }
  40. func actionDone() {
  41. viewDelegate?.actionDone()
  42. }
  43. func actionPrev() {
  44. viewDelegate?.actionPrev()
  45. }
  46. func actionNext() {
  47. viewDelegate?.actionNext()
  48. }
  49. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注