温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS 中Swift仿微信添加提示小红点功能(无数字)

发布时间:2020-09-20 21:47:03 来源:脚本之家 阅读:333 作者:LinXunFeng 栏目:移动开发

具体内容详情如下所示:

  • 以分类的方式实现

代码

UITabBar+Extenstion.swift

fileprivate let lxfFlag: Int = 666
extension UITabBar {
 // MARK:- 显示小红点
 func showBadgOn(index itemIndex: Int, tabbarItemNums: CGFloat = 4.0) {
  // 移除之前的小红点
  self.removeBadgeOn(index: itemIndex)
  // 创建小红点
  let bageView = UIView()
  bageView.tag = itemIndex + lxfFlag
  bageView.layer.cornerRadius = 5
  bageView.backgroundColor = UIColor.red
  let tabFrame = self.frame
  // 确定小红点的位置
  let percentX: CGFloat = (CGFloat(itemIndex) + 0.59) / tabbarItemNums
  let x: CGFloat = CGFloat(ceilf(Float(percentX * tabFrame.size.width)))
  let y: CGFloat = CGFloat(ceilf(Float(0.115 * tabFrame.size.height)))
  bageView.frame = CGRect(x: x, y: y, width: 10, height: 10)
  self.addSubview(bageView)
 }
 // MARK:- 隐藏小红点
 func hideBadg(on itemIndex: Int) {
  // 移除小红点
  self.removeBadgeOn(index: itemIndex)
 }
 // MARK:- 移除小红点
 fileprivate func removeBadgeOn(index itemIndex: Int) {
  // 按照tag值进行移除
  _ = subviews.map {
   if $0.tag == itemIndex + lxfFlag {
    $0.removeFromSuperview()
   }
  }
 }
}

使用

// 默认4个tabbarItem
self.tabBarController?.tabBar.showBadgOn(index: 2)
// 如果不是则用这个方法
// self.tabBarController?.tabBar.showBadgOn(index: Int, tabbarItemNums: CGFloat)

效果

iOS 中Swift仿微信添加提示小红点功能(无数字)

小红点

以上所述是小编给大家介绍的iOS 中Swift仿微信添加提示小红点功能(无数字),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI