ios - 如何为按钮动态添加角标(Badge)?

标签 ios swift xlpagertabstrip

我想在收到来自 DB 的新更新时向选项卡添加角标(Badge)。有没有我可以调用的方法来更新 IndicatorInfo

无法弄清楚请帮助。

最佳答案

我不得不使用 XIB 文件,手动设置约束和处理案例。

执行的步骤

  1. ButtonCell.xib 文件中的 UIImageView 添加约束,使其与 UILabel 对齐。 (我将它们添加到 UIView 以确保 View 居中。)
  2. 更改为 ButtonBaragerTabStripViewController 中的以下代码

    buttonBarItemSpec = .nibFile(nibName: "ButtonCell", bundle: Bundle(for: ButtonBarViewCell.self), width: { [weak self] (childItemInfo) -> CGFloat in
                let label = UILabel()
                label.translatesAutoresizingMaskIntoConstraints = false
                label.font = self?.settings.style.buttonBarItemFont
                label.text = childItemInfo.title
                let labelSize = label.intrinsicContentSize
            return labelSize.width + (childItemInfo.image != nil ? 35.0 : 0.0) + (self?.settings.style.buttonBarItemLeftRightMargin ?? 8) * 2
        })
    
  3. 添加了约束 @IBOutlet 并将它的 constant 值设置为 0.0 当图像不应该存在时。

关于ios - 如何为按钮动态添加角标(Badge)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46902248/

相关文章:

ios - 当 View 加载到一个 xib 文件中时选择特定的 UIView

ios - 如何将 InAppPurchase 设置放入设置包中?

ios - 水平移动 backButtonImage

ios - 扫描特定服务 CBUUID 时找不到外围设备

ios - Facebook 分享来自 iOS 应用程序的图像

iOS SwiftUI : prevent resizable() animation

ios - SplitViewControllers 可以在 iPhone 上使用吗?

ios - 使用 iOS 库时 Storyboard View 未连接

ios - Swift:UIScrollView 不使用 XLPagerTabStrip 显示 subview Controller