ESTabBarItem.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // ESTabBarController.swift
  3. //
  4. // Created by Vincent Li on 2017/2/8.
  5. // Copyright (c) 2013-2018 ESTabBarController (https://github.com/eggswift/ESTabBarController)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. import UIKit
  26. /*
  27. * ESTabBarItem继承自UITabBarItem,目的是为ESTabBarItemContentView提供UITabBarItem属性的设置。
  28. * 目前支持大多常用的属性,例如image, selectedImage, title, tag 等。
  29. *
  30. * Unsupport properties:
  31. * MARK: UIBarItem properties
  32. * 1. var isEnabled: Bool
  33. * 2. var landscapeImagePhone: UIImage?
  34. * 3. var imageInsets: UIEdgeInsets
  35. * 4. var landscapeImagePhoneInsets: UIEdgeInsets
  36. * 5. func setTitleTextAttributes(_ attributes: [String : Any]?, for state: UIControlState)
  37. * 6. func titleTextAttributes(for state: UIControlState) -> [String : Any]?
  38. * MARK: UITabBarItem properties
  39. * 7. var titlePositionAdjustment: UIOffset
  40. * 8. func setBadgeTextAttributes(_ textAttributes: [String : Any]?, for state: UIControlState)
  41. * 9. func badgeTextAttributes(for state: UIControlState) -> [String : Any]?
  42. */
  43. @available(iOS 8.0, *)
  44. open class ESTabBarItem: UITabBarItem {
  45. /// Customize content view
  46. open var contentView: ESTabBarItemContentView?
  47. // MARK: UIBarItem properties
  48. open override var title: String? // default is nil
  49. {
  50. didSet { self.contentView?.title = title }
  51. }
  52. open override var image: UIImage? // default is nil
  53. {
  54. didSet { self.contentView?.image = image }
  55. }
  56. // MARK: UITabBarItem properties
  57. open override var selectedImage: UIImage? // default is nil
  58. {
  59. didSet { self.contentView?.selectedImage = selectedImage }
  60. }
  61. open override var badgeValue: String? // default is nil
  62. {
  63. get { return contentView?.badgeValue }
  64. set(newValue) { contentView?.badgeValue = newValue }
  65. }
  66. /// Override UITabBarItem.badgeColor, make it available for iOS8.0 and later.
  67. /// If this item displays a badge, this color will be used for the badge's background. If set to nil, the default background color will be used instead.
  68. @available(iOS 8.0, *)
  69. open override var badgeColor: UIColor? {
  70. get { return contentView?.badgeColor }
  71. set(newValue) { contentView?.badgeColor = newValue }
  72. }
  73. open override var tag: Int // default is 0
  74. {
  75. didSet { self.contentView?.tag = tag }
  76. }
  77. /* The unselected image is autogenerated from the image argument. The selected image
  78. is autogenerated from the selectedImage if provided and the image argument otherwise.
  79. To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal (see UIImage.h)
  80. */
  81. public init(_ contentView: ESTabBarItemContentView = ESTabBarItemContentView(), title: String? = nil, image: UIImage? = nil, selectedImage: UIImage? = nil, tag: Int = 0) {
  82. super.init()
  83. self.contentView = contentView
  84. self.setTitle(title, image: image, selectedImage: selectedImage, tag: tag)
  85. }
  86. public required init?(coder aDecoder: NSCoder) {
  87. fatalError("init(coder:) has not been implemented")
  88. }
  89. open func setTitle(_ title: String? = nil, image: UIImage? = nil, selectedImage: UIImage? = nil, tag: Int = 0) {
  90. self.title = title
  91. self.image = image
  92. self.selectedImage = selectedImage
  93. self.tag = tag
  94. }
  95. }