一次比较懒的想法,不想自定义UITabBar,也不想用第三方框架,因而想尝试修改苹果私有类来达到部分效果git
在swift3及xcode8 beta2 环境下github
运行时遍历出成员变量swift
var count:UInt32 = 0 let ivarlist = class_copyIvarList(NSClassFromString("UITabBarButton")!, &count) for index in 0..<numericCast(count) { let ivar = ivarlist![index] let ivarStr = String.init(utf8String: ivar_getName(ivar!)) print("\(ivarStr)") }
Optional("_hitRect") Optional("_info") Optional("_vibrancyEffectView") Optional("_label") Optional("_badge") Optional("_selectedIndicator") Optional("_selected") Optional("_infoInsets") Optional("_selectedInfoOffset") Optional("_infoOffset") Optional("_customSelectedIndicatorImage") Optional("_labelOffset") Optional("_buttonTintColorsForState") Optional("_contentTintColorsForState") Optional("_badgeColor") Optional("_badgeTextAttributesForState") Optional("_showsHighlightedState") Optional("_centerAllContents") Optional("_appearanceGuideClass") Optional("_tabBar")
其中 "_info" ,"_label" ,"_badge" 这三个是咱们须要的xcode
二、"_badge"是什么呢?它真实类型是什么,子控件及属性名是什么app
let tab = window?.rootViewController as! TabBarViewController for view in tab.tabBar.subviews { if view.isKind(of: NSClassFromString("UITabBarButton")! ) { let badgeView = view.value(forKeyPath: "_badge") print("1\(badgeView!.self)") let badge = badgeView as! UIView print("2\(badge.subviews)") } } }
1 <_UIBadgeView: 0x7fd671c336c0; frame = (33 2; 18 18); text = '1'; userInteractionEnabled = NO; layer = <CALayer: 0x608000031240>> 2 [<UIImageView: 0x7fd671c09570; frame = (0 0; 18 18); opaque = NO; userInteractionEnabled = NO; tintColor = UIExtendedSRGBColorSpace 1 0.231373 0.188235 1; layer = <CALayer: 0x60800002e9a0>>, <UILabel: 0x7fd671c33ba0; frame = (6 1; 6 16); text = '1'; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x60800009c890>>]
var count:UInt32 = 0 let ivarlist = class_copyIvarList(NSClassFromString("_UIBadgeView")!, &count) for index in 0..<numericCast(count) { let ivar = ivarlist![index] let ivarStr = String.init(utf8String: ivar_getName(ivar!)) print("badge= \(ivarStr)") } badge= Optional("_label") badge= Optional("_background") badge= Optional("_mergedTextAttributes") badge= Optional("_text") badge= Optional("_textAttributes") badge= Optional("_backgroundColor")
自定义TabBarController里,添加4个子控制器,其中第三个添加一个空的UIViewController,图片为UIImage(), 空的TabBarItem,只是让这个系统生成的UITabBarButton占好位置框架
自定义UIButton,添加到TabBar上,盖住上述 空的UITabBarButton上ide
appDelegate成为TabBarController的代理,监控tabBar的点击学习
extension AppDelegate: UITabBarControllerDelegate { func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) { for view in tabBarController.tabBar.subviews { if view.isKind(of: NSClassFromString("UITabBarButton")! ) { // UITabBarButton是继承UIControl的, 经过 按钮的状态 才判别是不是如今点击的按钮 let state = view.value(forKeyPath: "highlighted") let stateValue = state as! Int if stateValue == 1 { // 拿到当前点击的 按钮的imageView let imageView = view.value(forKeyPath: "_info") guard let temp = imageView else { return } let tabButtonImgView = temp as! UIImageView // 添加动画 UIView.animate(withDuration: 0.2, animations: { tabButtonImgView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3) }, completion: { (Bool) in tabButtonImgView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) }) } } } } }
因为UITabBarButton的子控件都是懒加载的,须要用控制器的tabBarItem 模型对UITabBarButton进行设置动画
for button in tabBar.subviews { if button.isKind(of: NSClassFromString("UITabBarButton")! ) { let label = button.value(forKeyPath: "_label") as! UILabel // print("\(label.text)") if label.text == "" { // 经过label.text 肯定是购物车按钮 那位置的 UITabBarButton let badgeButton = button.value(forKeyPath: "_badge") // print("\(badgeButton!.self)") guard let badgeView = badgeButton else { return } let badge = badgeView as! UIView // print("\(badge.subviews)") badge.frame.origin.x = 52 // 可根据须要计算 } } }
let tab = window?.rootViewController as! TabBarViewController for button in tab.tabBar.subviews { if button.isKind(of: NSClassFromString("UITabBarButton")! ) { let label = button.value(forKeyPath: "_label") as! UILabel // 外界经过 lable.text 区别出是咱们须要的 第三个UITabBarButton if label.text == "" { let badgeButton = button.value(forKeyPath: "_badge") guard let badgeView = badgeButton else { return } // 拿到 badge 控件 let badge = badgeView as! UIView // num变量为 点击加号按钮, 商品个数 计量 let str:NSString = NSString.init(format: "%d", num) // kvc 设置属性 badgeView.setValue(UIFont.systemFont(ofSize: 9), forKeyPath: "_label.font") badgeView.setValue(str, forKeyPath: "_label.text") let tabLabal = badgeView.value(forKeyPath: "_label") let label = tabLabal as! UILabel // 拿到lebel 计算label 的大小、尺寸 let size = str.size(attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: 9)]) label.frame = CGRect(x: (badge.frame.size.width - size.width) * 0.5, y: (badge.frame.size.height - size.height) * 0.5, width: size.width, height: size.height) // 添加核心动画 let scaleAni = CABasicAnimation() scaleAni.keyPath = "transform.scale" scaleAni.fromValue = 1.0 scaleAni.toValue = 1.2 scaleAni.autoreverses = true scaleAni.duration = 0.25 badge.layer.add(scaleAni, forKey: nil) } } }