Skip to content

Commit c454e05

Browse files
author
vincentttli
committed
Support for titlePositionAdjustment #197
1 parent 08e992c commit c454e05

24 files changed

+209
-120
lines changed

ESTabBarControllerExample/ESTabBarControllerExample.xcodeproj/project.pbxproj

+56-8
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,55 @@
129129
/* End PBXFrameworksBuildPhase section */
130130

131131
/* Begin PBXGroup section */
132+
7594959025875BF100C80CF6 /* Basic */ = {
133+
isa = PBXGroup;
134+
children = (
135+
F6858F4D1E4F516F002DAF6C /* ExampleBasicContentView.swift */,
136+
);
137+
path = Basic;
138+
sourceTree = "<group>";
139+
};
140+
7594959125875C0200C80CF6 /* Background */ = {
141+
isa = PBXGroup;
142+
children = (
143+
F6858F4C1E4F516F002DAF6C /* ExampleBackgroundContentView.swift */,
144+
);
145+
path = Background;
146+
sourceTree = "<group>";
147+
};
148+
7594959325885FC400C80CF6 /* Bounces */ = {
149+
isa = PBXGroup;
150+
children = (
151+
F6858F4E1E4F516F002DAF6C /* ExampleBouncesContentView.swift */,
152+
);
153+
path = Bounces;
154+
sourceTree = "<group>";
155+
};
156+
7594959425885FCA00C80CF6 /* Tips */ = {
157+
isa = PBXGroup;
158+
children = (
159+
F6858F511E4F516F002DAF6C /* ExampleTipsContentView.swift */,
160+
F6858F4B1E4F516F002DAF6C /* ExampleAnimateTipsContentView.swift */,
161+
);
162+
path = Tips;
163+
sourceTree = "<group>";
164+
};
165+
7594959525885FD400C80CF6 /* Highlight */ = {
166+
isa = PBXGroup;
167+
children = (
168+
F6858F4F1E4F516F002DAF6C /* ExampleHighlightableContentView.swift */,
169+
);
170+
path = Highlight;
171+
sourceTree = "<group>";
172+
};
173+
7594959725885FEA00C80CF6 /* Irregularity */ = {
174+
isa = PBXGroup;
175+
children = (
176+
F6858F501E4F516F002DAF6C /* ExampleIrregularityContentView.swift */,
177+
);
178+
path = Irregularity;
179+
sourceTree = "<group>";
180+
};
132181
F6858F3E1E4F435C002DAF6C /* Lottie */ = {
133182
isa = PBXGroup;
134183
children = (
@@ -143,13 +192,13 @@
143192
F6858F4A1E4F516F002DAF6C /* Content */ = {
144193
isa = PBXGroup;
145194
children = (
146-
F6858F4B1E4F516F002DAF6C /* ExampleAnimateTipsContentView.swift */,
147-
F6858F4C1E4F516F002DAF6C /* ExampleBackgroundContentView.swift */,
148-
F6858F4D1E4F516F002DAF6C /* ExampleBasicContentView.swift */,
149-
F6858F4E1E4F516F002DAF6C /* ExampleBouncesContentView.swift */,
150-
F6858F4F1E4F516F002DAF6C /* ExampleHighlightableContentView.swift */,
151-
F6858F501E4F516F002DAF6C /* ExampleIrregularityContentView.swift */,
152-
F6858F511E4F516F002DAF6C /* ExampleTipsContentView.swift */,
195+
7594959025875BF100C80CF6 /* Basic */,
196+
7594959125875C0200C80CF6 /* Background */,
197+
7594959525885FD400C80CF6 /* Highlight */,
198+
7594959325885FC400C80CF6 /* Bounces */,
199+
7594959725885FEA00C80CF6 /* Irregularity */,
200+
7594959425885FCA00C80CF6 /* Tips */,
201+
F6858F3E1E4F435C002DAF6C /* Lottie */,
153202
);
154203
path = Content;
155204
sourceTree = "<group>";
@@ -182,7 +231,6 @@
182231
F6EE45A31E4C4D72001E0FA1 /* ExampleViewController.swift */,
183232
F6EE45A41E4C4D72001E0FA1 /* ExampleNavigationController.swift */,
184233
F6858F4A1E4F516F002DAF6C /* Content */,
185-
F6858F3E1E4F435C002DAF6C /* Lottie */,
186234
F6EE45A71E4C4DA3001E0FA1 /* Me */,
187235
F6EE455B1E4AC173001E0FA1 /* Main.storyboard */,
188236
F6EE455E1E4AC173001E0FA1 /* Assets.xcassets */,

Sources/ESTabBar.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// ESTabBar.swift
33
//
44
// 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)
66
//
77
// Permission is hereby granted, free of charge, to any person obtaining a copy
88
// of this software and associated documentation files (the "Software"), to deal
@@ -268,8 +268,8 @@ internal extension ESTabBar /* Actions */ {
268268
self.addSubview(container)
269269
self.containers.append(container)
270270

271-
if let item = item as? ESTabBarItem, let contentView = item.contentView {
272-
container.addSubview(contentView)
271+
if let item = item as? ESTabBarItem {
272+
container.addSubview(item.contentView)
273273
}
274274
if self.isMoreItem(idx), let moreContentView = moreContentView {
275275
container.addSubview(moreContentView)
@@ -294,7 +294,7 @@ internal extension ESTabBar /* Actions */ {
294294
}
295295

296296
if let item = item as? ESTabBarItem {
297-
item.contentView?.highlight(animated: true, completion: nil)
297+
item.contentView.highlight(animated: true, completion: nil)
298298
} else if self.isMoreItem(newIndex) {
299299
moreContentView?.highlight(animated: true, completion: nil)
300300
}
@@ -314,7 +314,7 @@ internal extension ESTabBar /* Actions */ {
314314
}
315315

316316
if let item = item as? ESTabBarItem {
317-
item.contentView?.dehighlight(animated: true, completion: nil)
317+
item.contentView.dehighlight(animated: true, completion: nil)
318318
} else if self.isMoreItem(newIndex) {
319319
moreContentView?.dehighlight(animated: true, completion: nil)
320320
}
@@ -342,8 +342,8 @@ internal extension ESTabBar /* Actions */ {
342342
customDelegate?.tabBar(self, didHijack: item)
343343
if animated {
344344
if let item = item as? ESTabBarItem {
345-
item.contentView?.select(animated: animated, completion: {
346-
item.contentView?.deselect(animated: false, completion: nil)
345+
item.contentView.select(animated: animated, completion: {
346+
item.contentView.deselect(animated: false, completion: nil)
347347
})
348348
} else if self.isMoreItem(newIndex) {
349349
moreContentView?.select(animated: animated, completion: {
@@ -357,19 +357,19 @@ internal extension ESTabBar /* Actions */ {
357357
if currentIndex != newIndex {
358358
if currentIndex != -1 && currentIndex < items?.count ?? 0{
359359
if let currentItem = items?[currentIndex] as? ESTabBarItem {
360-
currentItem.contentView?.deselect(animated: animated, completion: nil)
360+
currentItem.contentView.deselect(animated: animated, completion: nil)
361361
} else if self.isMoreItem(currentIndex) {
362362
moreContentView?.deselect(animated: animated, completion: nil)
363363
}
364364
}
365365
if let item = item as? ESTabBarItem {
366-
item.contentView?.select(animated: animated, completion: nil)
366+
item.contentView.select(animated: animated, completion: nil)
367367
} else if self.isMoreItem(newIndex) {
368368
moreContentView?.select(animated: animated, completion: nil)
369369
}
370370
} else if currentIndex == newIndex {
371371
if let item = item as? ESTabBarItem {
372-
item.contentView?.reselect(animated: animated, completion: nil)
372+
item.contentView.reselect(animated: animated, completion: nil)
373373
} else if self.isMoreItem(newIndex) {
374374
moreContentView?.reselect(animated: animated, completion: nil)
375375
}

Sources/ESTabBarController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// ESTabBarController.swift
33
//
44
// 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)
66
//
77
// Permission is hereby granted, free of charge, to any person obtaining a copy
88
// of this software and associated documentation files (the "Software"), to deal

Sources/ESTabBarItem.swift

+74-53
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// ESTabBarController.swift
33
//
44
// 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)
66
//
77
// Permission is hereby granted, free of charge, to any person obtaining a copy
88
// of this software and associated documentation files (the "Software"), to deal
@@ -25,83 +25,104 @@
2525

2626
import UIKit
2727

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+
///
4542
@available(iOS 8.0, *)
4643
open class ESTabBarItem: UITabBarItem {
4744

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+
}
5052

5153
// 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 }
5559
}
5660

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 }
6071
}
6172

6273
// 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 }
6680
}
6781

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 }
7287
}
7388

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.
7697
@available(iOS 8.0, *)
7798
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 }
80101
}
81102

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+
}
85112
}
86113

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)
91115
public init(_ contentView: ESTabBarItemContentView = ESTabBarItemContentView(), title: String? = nil, image: UIImage? = nil, selectedImage: UIImage? = nil, tag: Int = 0) {
92116
super.init()
93117
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
98122
}
99123

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)
105126
}
106127

107128
}

Sources/ESTabBarItemBadgeView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// ESTabBarItemBadgeView.swift
33
//
44
// 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)
66
//
77
// Permission is hereby granted, free of charge, to any person obtaining a copy
88
// of this software and associated documentation files (the "Software"), to deal

Sources/ESTabBarItemContainer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// ESTabBarItemContainer.swift
33
//
44
// 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)
66
//
77
// Permission is hereby granted, free of charge, to any person obtaining a copy
88
// of this software and associated documentation files (the "Software"), to deal
@@ -39,7 +39,7 @@ internal class ESTabBarItemContainer: UIControl {
3939
}
4040

4141
internal required init?(coder aDecoder: NSCoder) {
42-
fatalError("init(coder:) has not been implemented")
42+
super.init(coder: aDecoder)
4343
}
4444

4545
internal override func layoutSubviews() {

0 commit comments

Comments
 (0)