8
8
9
9
import UIKit
10
10
11
- internal let HHTabBarViewHeight = CGFloat ( 49.0 )
11
+ fileprivate let hhTabBarViewHeight = CGFloat ( 49.0 )
12
12
13
13
///Animation Types for Tab Changes.
14
14
public enum HHTabBarTabChangeAnimationType {
@@ -22,7 +22,7 @@ public class HHTabBarView: UIView {
22
22
public static var shared = HHTabBarView . init ( )
23
23
24
24
///For Internal Navigation
25
- public var referenceUITabBarController = UITabBarController . init ( )
25
+ private ( set ) public var referenceUITabBarController = UITabBarController . init ( )
26
26
27
27
//MARK: Setters
28
28
///Animation Type
@@ -31,15 +31,15 @@ public class HHTabBarView: UIView {
31
31
///Set HHTabButton for HHTabBarView.
32
32
public var tabBarTabs = Array < HHTabButton > ( ) {
33
33
didSet {
34
- createTabs ( )
34
+ self . createTabs ( )
35
35
}
36
36
}
37
37
38
38
///Set the default tab for HHTabBarView.
39
39
public var defaultIndex = 0 {
40
40
didSet {
41
- if areTabsCreated ( ) {
42
- selectTabAtIndex ( withIndex: defaultIndex)
41
+ if self . areTabsCreated ( ) {
42
+ self . selectTabAtIndex ( withIndex: defaultIndex)
43
43
}
44
44
}
45
45
}
@@ -48,13 +48,13 @@ public class HHTabBarView: UIView {
48
48
///Specify indexes of tabs to lock. [0, 2, 3]
49
49
public var lockTabIndexes = Array < Int > ( ) {
50
50
didSet {
51
- lockUnlockTabs ( )
51
+ self . lockUnlockTabs ( )
52
52
}
53
53
}
54
54
55
55
///Update Badge Value for Specific Tab.
56
56
public func updateBadge( forTabIndex index: Int , withValue value: Int ) -> Void {
57
- if areTabsCreated ( ) {
57
+ if self . areTabsCreated ( ) {
58
58
let hhTabButton = tabBarTabs [ index]
59
59
hhTabButton. badgeValue = value
60
60
}
@@ -75,28 +75,36 @@ public class HHTabBarView: UIView {
75
75
76
76
required
77
77
convenience public init ( ) {
78
- self . init ( frame: CGRect . init ( x: 0.0 , y: 0.0 , width: UIScreen . main. bounds. size. width, height: HHTabBarViewHeight ) )
78
+ self . init ( frame: CGRect . init ( x: 0.0 , y: 0.0 , width: UIScreen . main. bounds. size. width, height: hhTabBarViewHeight ) )
79
79
//You can configure it to any background color you want.
80
80
self . backgroundColor = . clear
81
81
//For Portrait/Landscape.
82
82
self . autoresizingMask = [ . flexibleWidth, . flexibleBottomMargin, . flexibleLeftMargin, . flexibleRightMargin]
83
83
//Adding to UITabBarController's subview.
84
- referenceUITabBarController. view. addSubview ( self )
84
+ self . referenceUITabBarController. view. addSubview ( self )
85
85
//This is important otherwise tabBar will be visible if tabChangeAnimationType = .flash
86
- referenceUITabBarController. tabBar. isHidden = true
86
+ self . referenceUITabBarController. tabBar. isHidden = true
87
87
}
88
88
89
89
//HHTabBarViewFrame Frame
90
- fileprivate func HHTabBarViewFrame ( ) -> CGRect {
90
+ fileprivate func getHHTabBarViewFrame ( ) -> CGRect {
91
91
let screenSize = UIScreen . main. bounds. size
92
92
let screentHeight = screenSize. height
93
93
let screentWidth = screenSize. width
94
- return CGRect . init ( x: 0.0 , y: ( screentHeight - HHTabBarViewHeight) , width: screentWidth, height: HHTabBarViewHeight)
94
+ var tabBarHeight = hhTabBarViewHeight
95
+
96
+ //To support UI for iPhone X
97
+ if #available( iOS 11 . 0 , * ) {
98
+ let bottomPadding = self . referenceUITabBarController. tabBar. safeAreaInsets. bottom
99
+ tabBarHeight += bottomPadding
100
+ }
101
+
102
+ return CGRect . init ( x: 0.0 , y: ( screentHeight - tabBarHeight) , width: screentWidth, height: tabBarHeight)
95
103
}
96
104
97
105
//UI Updates
98
106
public override func layoutSubviews( ) {
99
- self . frame = HHTabBarViewFrame ( )
107
+ self . frame = getHHTabBarViewFrame ( )
100
108
}
101
109
102
110
//Helper to Select a Particular Tab.
@@ -113,18 +121,18 @@ public class HHTabBarView: UIView {
113
121
}
114
122
115
123
// Apply Tab Changes in UITabBarController
116
- referenceUITabBarController. selectedIndex = tabIndex
124
+ self . referenceUITabBarController. selectedIndex = tabIndex
117
125
118
126
// Lock or Unlock the Tabs if requires.
119
- lockUnlockTabs ( )
127
+ self . lockUnlockTabs ( )
120
128
121
129
let currentHHTabButton = tabBarTabs [ tabIndex]
122
130
currentHHTabButton. isUserInteractionEnabled = false
123
131
}
124
132
125
133
//Check if Tabs are created.
126
134
fileprivate func areTabsCreated( ) -> Bool {
127
- if !tabBarTabs. isEmpty {
135
+ if !self . tabBarTabs. isEmpty {
128
136
return true
129
137
}
130
138
return false
@@ -139,8 +147,8 @@ public class HHTabBarView: UIView {
139
147
}
140
148
141
149
//Then Lock the provided Tab Indexes.
142
- if !lockTabIndexes. isEmpty {
143
- for index in lockTabIndexes {
150
+ if !self . lockTabIndexes. isEmpty {
151
+ for index in self . lockTabIndexes {
144
152
let hhTabButton = tabBarTabs [ index]
145
153
hhTabButton. isUserInteractionEnabled = false
146
154
}
@@ -155,7 +163,7 @@ public class HHTabBarView: UIView {
155
163
156
164
let width = CGFloat ( self . frame. size. width) / CGFloat( tabBarTabs. count)
157
165
let height = self . frame. size. height
158
-
166
+
159
167
for hhTabButton in tabBarTabs {
160
168
hhTabButton. frame = CGRect . init ( x: xPos, y: yPos, width: width, height: height)
161
169
hhTabButton. addTarget ( self , action: #selector( actionTabTapped ( tab: ) ) , for: . touchUpInside)
@@ -170,16 +178,16 @@ public class HHTabBarView: UIView {
170
178
171
179
//Actions
172
180
@objc fileprivate func actionTabTapped( tab: HHTabButton ) {
173
- if onTabTapped != nil {
174
- animateTabBarButton ( tabBarButton: tab)
181
+ if self . onTabTapped != nil {
182
+ self . animateTabBarButton ( tabBarButton: tab)
175
183
self . selectTabAtIndex ( withIndex: tab. tabIndex)
176
184
self . onTabTapped ( tab. tabIndex)
177
185
}
178
186
}
179
187
180
188
//Perform Animation on Tab Changes.
181
189
fileprivate func animateTabBarButton( tabBarButton: HHTabButton ) {
182
- switch tabChangeAnimationType {
190
+ switch self . tabChangeAnimationType {
183
191
case . flash:
184
192
tabBarButton. flash ( )
185
193
break
0 commit comments