用 Swift 4.0 编写的 iOS 自定义标签栏控制器
截图
安装
Cocoa Pods
pod 'AZTabBar'
手动
简单地将 Sources
文件夹拖放到你的项目中。
使用方法
创建一个 String/UIImage 数组
//The icons that will be displayed on the tabs that are not currently selected
var icons = [String]()
icons.append("ic_star_outline")
icons.append("ic_history_outline")
icons.append("ic_phone_outline")
icons.append("ic_chat_outline")
icons.append("ic_settings_outline")
//The icons that will be displayed for each tab once they are selected.
var selectedIcons = [String]()
selectedIcons.append("ic_star_filled")
selectedIcons.append("ic_history_filled")
selectedIcons.append("ic_phone_filled")
selectedIcons.append("ic_chat_filled")
selectedIcons.append("ic_settings_filled")
然后通过以下方法初始化控制器对象:
tabController = .insert(into: self, withTabIconNames: icons, andSelectedIconNames: selectedIcons)
添加控制器
//if you are using storyboard:
let myChildViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChildViewController")!
//if you are loading programmatically:
let myChildViewController = ChildViewController()
tabController.setViewController(myChildViewController, atIndex: 0)
添加操作
tabController.setAction(atIndex: 3) {
//Your statments
print("Hello World")
}
请注意,您可以在某个索引处添加操作和视图控制器。
自定义
//default color of the icons on the buttons
tabController.defaultColor = .white
//the color of the icon when a menu is selected
tabController.selectedColor = .orange
//The color of the icon of a highlighted tab
tabController.highlightColor = .white
//The background color of the button of the highlighted tabs.
tabController.highlightedBackgroundColor = .green
//The background color of the tab bar
tabController.buttonsBackgroundColor = .black
//The color of the selection indicator.
tabController.selectionIndicatorColor = .green
// default is 3.0
tabController.selectionIndicatorHeight = 0
// change the seperator line color
tabController.separatorLineColor = .black
//hide or show the seperator line
tabController.separatorLineVisible = false
//Enable tab change animation that looks like facebook
tabController.animateTabChange = true
额外功能
使标签页看起来高亮
tabController.highlightButton(atIndex: 2)
隐藏/显示标签栏
tabController.setBar(hidden: true, animated: true)
向菜单添加徽章(使用nil值移除现有徽章)
tabController.setBadgeText("5", atIndex: 3)
程序化切换到特定标签
tabController.setIndex(2) //animated = true by default
//or
tabController.setIndex(2, animated: false)
授予权限以按标签修改状态栏样式
override var childViewControllerForStatusBarStyle: UIViewController?{
return tabController
}
//Then implement the delegate method:
func tabBar(_ tabBar: AZTabBarController, statusBarStyleForIndex index: Int) -> UIStatusBarStyle {
return (index % 2) == 0 ? .default : .lightContent
}
管理标题
添加标题
tabController.setTitle("Home", atIndex: 0)
tabController.setTitle("Search", atIndex: 1)
tabController.setTitle("Camera", atIndex: 2)
tabController.setTitle("Feed", atIndex: 3)
tabController.setTitle("Profile", atIndex: 4)
只为选定索引显示标题
tabController.onlyShowTextForSelectedButtons = true
从子视图控制器访问AZTabBarController
我为UIViewController
创建了一个扩展,它增加了一个名为currentTabBar
的变量
public var currentTabBar: AZTabBarController? { get }
可以像这样调用它
currentTabBar?.setBadgeText("New Badge Value",atIndex: 2)
代理方法
以下是 AZTabBarDelegate 的功能
/// This function is called after `didMoveToTabAtIndex` is called. In order for this function to work you must override the var `childViewControllerForStatusBarStyle` in the root controller to return this instance of AZTabBarController.
///
/// - Parameters:
/// - tabBar: The current instance of AZTabBarController.
/// - index: The index of the child view controller which you wish to set a status bar style for.
/// - Returns: The status bar style.
func tabBar(_ tabBar: AZTabBarController, statusBarStyleForIndex index: Int)-> UIStatusBarStyle
/// This function is called whenever user clicks the menu a long click. If returned false, the action will be ignored.
///
/// - Parameters:
/// - tabBar: The current instance of AZTabBarController.
/// - index: The index of the child view controller which you wish to disable the long menu click for.
/// - Returns: true if you wish to allow long-click interaction for a specific tab, false otherwise.
func tabBar(_ tabBar: AZTabBarController, shouldLongClickForIndex index: Int)-> Bool
/// This function is used to enable/disable animation for a certian tab.
///
/// - Parameters:
/// - tabBar: The current instance of AZTabBarController.
/// - index: The index of the tab.
/// - Returns: true if you wish to enable the animation, false otherwise.
func tabBar(_ tabBar: AZTabBarController, shouldAnimateButtonInteractionAtIndex index:Int)->Bool
/// This function is called whenever user taps one of the menu buttons.
///
/// - Parameters:
/// - tabBar: The current instance of AZTabBarController.
/// - index: The index of the menu the user tapped.
func tabBar(_ tabBar: AZTabBarController, didSelectTabAtIndex index: Int)
/// This function is called whenever user taps and hold one of the menu buttons. Note that this function will not be called for a certain index if `shouldLongClickForIndex` is implemented and returns false for that very same index.
///
/// - Parameters:
/// - tabBar: The current instance of AZTabBarController.
/// - index: The index of the menu the user long clicked.
func tabBar(_ tabBar: AZTabBarController, didLongClickTabAtIndex index:Int)
/// This function is called before the child view controllers are switched.
///
/// - Parameters:
/// - tabBar: The current instance of AZTabBarController.
/// - index: The index of the controller which the tab bar will be switching to.
func tabBar(_ tabBar: AZTabBarController, willMoveToTabAtIndex index:Int)
/// This function is called after the child view controllers are switched.
///
/// - Parameters:
/// - tabBar: The current instance of AZTabBarController.
/// - index: The index of the controller which the tab bar had switched to.
func tabBar(_ tabBar: AZTabBarController, didMoveToTabAtIndex index: Int)
鸣谢
AZTabBarController最初受到ESTabBarController的启发,该控制器是用Objective-C编写的,由ezescaruli创建。
感谢Tobaloidee制作了Logo。