您的位置:首页 > 移动开发 > Swift

Swift中给UIView添加Badge

2015-09-02 01:30 411 查看
extension UIView{
//任意UIView添加badge
func showBadgeValue(#strBadgeValue: String) -> Void{

let tabBar = UITabBar(frame: CGRectMake(0, 0, 320, 50))
let item = UITabBarItem(title: "", image: nil, tag: 0)
item.badgeValue = strBadgeValue
let array = [item]

tabBar.items = array
for viewTab in tabBar.subviews{
for subview in viewTab.subviews{
let strClassName = String(UTF8String: object_getClassName(subview))
if strClassName == "UITabBarButtonBadge" || strClassName == "_UIBadgeView"{
let theSubView = subview as! UIView
theSubView.removeFromSuperview()
self.addSubview(theSubView)
theSubView.frame = CGRectMake(self.frame.size.width - theSubView.frame.size.width, 0, theSubView.frame.size.width, theSubView.frame.size.height)

}

}
}
}

//删除UIView的badge
func removeBadge() -> Void{
for subview in self.subviews{
let strClassName = String(UTF8String: object_getClassName(subview))
if strClassName == "UITabBarButtonBadge" || strClassName == "_UIBadgeView"{
let theSubView = subview as! UIView
theSubView.removeFromSuperview()
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: