|
2 | 2 | // ESTabBarController.swift
|
3 | 3 | //
|
4 | 4 | // Created by Vincent Li on 2017/2/8.
|
5 |
| -// Copyright (c) 2013-2018 ESTabBarController (https://github.com/eggswift/ESTabBarController) |
| 5 | +// Copyright (c) 2013-2020 ESTabBarController (https://github.com/eggswift/ESTabBarController) |
6 | 6 | //
|
7 | 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 | 8 | // of this software and associated documentation files (the "Software"), to deal
|
|
25 | 25 |
|
26 | 26 | import UIKit
|
27 | 27 |
|
28 |
| -/* |
29 |
| - * ESTabBarItem继承自UITabBarItem,目的是为ESTabBarItemContentView提供UITabBarItem属性的设置。 |
30 |
| - * 目前支持大多常用的属性,例如image, selectedImage, title, tag 等。 |
31 |
| - * |
32 |
| - * Unsupport properties: |
33 |
| - * MARK: UIBarItem properties |
34 |
| - * 1. var isEnabled: Bool |
35 |
| - * 2. var landscapeImagePhone: UIImage? |
36 |
| - * 3. var imageInsets: UIEdgeInsets |
37 |
| - * 4. var landscapeImagePhoneInsets: UIEdgeInsets |
38 |
| - * 5. func setTitleTextAttributes(_ attributes: [String : Any]?, for state: UIControlState) |
39 |
| - * 6. func titleTextAttributes(for state: UIControlState) -> [String : Any]? |
40 |
| - * MARK: UITabBarItem properties |
41 |
| - * 7. var titlePositionAdjustment: UIOffset |
42 |
| - * 8. func setBadgeTextAttributes(_ textAttributes: [String : Any]?, for state: UIControlState) |
43 |
| - * 9. func badgeTextAttributes(for state: UIControlState) -> [String : Any]? |
44 |
| - */ |
| 28 | +/// ESTabBarItem inherits from UITabBarItem, the purpose is to provide UITabBarItem property settings for ESTabBarItemContentView. |
| 29 | +/// Support most commonly used attributes, such as image, selectedImage, title, tag etc. |
| 30 | +/// |
| 31 | +/// Unsupport properties: |
| 32 | +/// MARK: UIBarItem properties |
| 33 | +/// 1. var landscapeImagePhone: UIImage? |
| 34 | +/// 2. var imageInsets: UIEdgeInsets |
| 35 | +/// 3. var landscapeImagePhoneInsets: UIEdgeInsets |
| 36 | +/// 4. func setTitleTextAttributes(_ attributes: [String : Any]?, for state: UIControlState) |
| 37 | +/// 5. func titleTextAttributes(for state: UIControlState) -> [String : Any]? |
| 38 | +/// MARK: UITabBarItem properties |
| 39 | +/// 1. func setBadgeTextAttributes(_ textAttributes: [String : Any]?, for state: UIControlState) |
| 40 | +/// 2. func badgeTextAttributes(for state: UIControlState) -> [String : Any]? |
| 41 | +/// |
45 | 42 | @available(iOS 8.0, *)
|
46 | 43 | open class ESTabBarItem: UITabBarItem {
|
47 | 44 |
|
48 |
| - /// Customize content view |
49 |
| - open var contentView: ESTabBarItemContentView? |
| 45 | + // MARK: UIView properties |
| 46 | + |
| 47 | + /// The receiver’s tag, an application-supplied integer that you can use to identify bar item objects in your application. default is `0` |
| 48 | + open override var tag: Int |
| 49 | + { |
| 50 | + didSet { self.contentView.tag = tag } |
| 51 | + } |
50 | 52 |
|
51 | 53 | // MARK: UIBarItem properties
|
52 |
| - open override var title: String? // default is nil |
53 |
| - { |
54 |
| - didSet { self.contentView?.title = title } |
| 54 | + |
| 55 | + /// A Boolean value indicating whether the item is enabled, default is `YES`. |
| 56 | + open override var isEnabled: Bool |
| 57 | + { |
| 58 | + didSet { self.contentView.enabled = isEnabled } |
55 | 59 | }
|
56 | 60 |
|
57 |
| - open override var image: UIImage? // default is nil |
58 |
| - { |
59 |
| - didSet { self.contentView?.image = image } |
| 61 | + /// The title displayed on the item, default is `nil` |
| 62 | + open override var title: String? |
| 63 | + { |
| 64 | + didSet { self.contentView.title = title } |
| 65 | + } |
| 66 | + |
| 67 | + /// The image used to represent the item, default is `nil` |
| 68 | + open override var image: UIImage? |
| 69 | + { |
| 70 | + didSet { self.contentView.image = image } |
60 | 71 | }
|
61 | 72 |
|
62 | 73 | // MARK: UITabBarItem properties
|
63 |
| - open override var selectedImage: UIImage? // default is nil |
64 |
| - { |
65 |
| - didSet { self.contentView?.selectedImage = selectedImage } |
| 74 | + |
| 75 | + /// The image displayed when the tab bar item is selected, default is `nil`. |
| 76 | + open override var selectedImage: UIImage? |
| 77 | + { |
| 78 | + get { return contentView.selectedImage } |
| 79 | + set(newValue) { contentView.selectedImage = newValue } |
66 | 80 | }
|
67 | 81 |
|
68 |
| - open override var badgeValue: String? // default is nil |
69 |
| - { |
70 |
| - get { return contentView?.badgeValue } |
71 |
| - set(newValue) { contentView?.badgeValue = newValue } |
| 82 | + /// Text that is displayed in the upper-right corner of the item with a surrounding red oval, default is `nil`. |
| 83 | + open override var badgeValue: String? |
| 84 | + { |
| 85 | + get { return contentView.badgeValue } |
| 86 | + set(newValue) { contentView.badgeValue = newValue } |
72 | 87 | }
|
73 | 88 |
|
74 |
| - /// Override UITabBarItem.badgeColor, make it available for iOS8.0 and later. |
75 |
| - /// 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. |
| 89 | + /// The offset to use to adjust the title position, default is `UIOffset.zero`. |
| 90 | + open override var titlePositionAdjustment: UIOffset |
| 91 | + { |
| 92 | + get { return contentView.titlePositionAdjustment } |
| 93 | + set(newValue) { contentView.titlePositionAdjustment = newValue } |
| 94 | + } |
| 95 | + |
| 96 | + /// The background color to apply to the badge, make it available for iOS8.0 and later. 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. |
76 | 97 | @available(iOS 8.0, *)
|
77 | 98 | open override var badgeColor: UIColor? {
|
78 |
| - get { return contentView?.badgeColor } |
79 |
| - set(newValue) { contentView?.badgeColor = newValue } |
| 99 | + get { return contentView.badgeColor } |
| 100 | + set(newValue) { contentView.badgeColor = newValue } |
80 | 101 | }
|
81 | 102 |
|
82 |
| - open override var tag: Int // default is 0 |
83 |
| - { |
84 |
| - didSet { self.contentView?.tag = tag } |
| 103 | + // MARK: ESTabBarItem properties |
| 104 | + |
| 105 | + /// Customize content view, default is `ESTabBarItemContentView` |
| 106 | + open var contentView: ESTabBarItemContentView = ESTabBarItemContentView() |
| 107 | + { |
| 108 | + didSet { |
| 109 | + self.contentView.updateLayout() |
| 110 | + self.contentView.updateDisplay() |
| 111 | + } |
85 | 112 | }
|
86 | 113 |
|
87 |
| - /* The unselected image is autogenerated from the image argument. The selected image |
88 |
| - is autogenerated from the selectedImage if provided and the image argument otherwise. |
89 |
| - To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal (see UIImage.h) |
90 |
| - */ |
| 114 | + /// The unselected image is autogenerated from the image argument. The selected image is autogenerated from the selectedImage if provided and the image argument otherwise. To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal (see UIImage.h) |
91 | 115 | public init(_ contentView: ESTabBarItemContentView = ESTabBarItemContentView(), title: String? = nil, image: UIImage? = nil, selectedImage: UIImage? = nil, tag: Int = 0) {
|
92 | 116 | super.init()
|
93 | 117 | self.contentView = contentView
|
94 |
| - self.setTitle(title, image: image, selectedImage: selectedImage, tag: tag) |
95 |
| - } |
96 |
| - public required init?(coder aDecoder: NSCoder) { |
97 |
| - fatalError("init(coder:) has not been implemented") |
| 118 | + self.contentView.title = title |
| 119 | + self.contentView.image = image |
| 120 | + self.contentView.selectedImage = selectedImage |
| 121 | + self.contentView.tag = tag |
98 | 122 | }
|
99 | 123 |
|
100 |
| - open func setTitle(_ title: String? = nil, image: UIImage? = nil, selectedImage: UIImage? = nil, tag: Int = 0) { |
101 |
| - self.title = title |
102 |
| - self.image = image |
103 |
| - self.selectedImage = selectedImage |
104 |
| - self.tag = tag |
| 124 | + public required init?(coder aDecoder: NSCoder) { |
| 125 | + super.init(coder: aDecoder) |
105 | 126 | }
|
106 | 127 |
|
107 | 128 | }
|
0 commit comments