在Swift中将一行作为选择指示符添加到UITabbarItem

在Swift中将一行作为选择指示符添加到UITabbarItem,第1张

在Swift中将一行作为选择指示符添加到UITabbarItem

我解决了我的问题。

这个小代码段的功能

  • 宽度是动态的
  • 它是动画的
  • 它对于将来的功能更具可定制性
        class FirstViewController: UIViewController {    let rectShape = CAShapeLayer()    let indicatorHeight: CGFloat = 5    var indicatorWidth: CGFloat!    let indicatorBottomMargin: CGFloat = 2    let indicatorLeftMargin: CGFloat = 2    override func viewDidLoad() {        super.viewDidLoad()        // setup tabbar indicator        rectShape.fillColor = UIColor.redColor().CGColor        indicatorWidth = view.bounds.maxX / 2 // count of items        self.tabBarController!.view.layer.addSublayer(rectShape)        self.tabBarController?.delegate = self        // initial position        updateTabbarIndicatorBySelectedTabIndex(0)    }    func updateTabbarIndicatorBySelectedTabIndex(index: Int) -> Void    {        let updatedBounds = CGRect( x: CGFloat(index) * (indicatorWidth + indicatorLeftMargin),   y: view.bounds.maxY - indicatorHeight,   width: indicatorWidth - indicatorLeftMargin,   height: indicatorHeight)        let path = CGPathCreateMutable()        CGPathAddRect(path, nil, updatedBounds)        rectShape.path = path    }    }    extension FirstViewController: UITabBarControllerDelegate {    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {        updateTabbarIndicatorBySelectedTabIndex(tabBarController.selectedIndex)    }    }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5620090.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存