这就是我的Tabbar现在的样子。
如何移动徽章,使其与钟形图标的右上方重叠?任何帮助都很感激。
a1o7rhls1#
下面是一个简单的红色斑点githubGist的例子。当然,如果你想在里面添加一个标签,你可以自定义你自己的badgeView = UIView()。
badgeView = UIView()
Swift 4.2
fileprivate let tabBarItemTag: Int = 10090 extension UITabBar { public func addItemBadge(atIndex index: Int) { guard let itemCount = self.items?.count, itemCount > 0 else { return } guard index < itemCount else { return } removeItemBadge(atIndex: index) let badgeView = UIView() badgeView.tag = tabBarItemTag + Int(index) badgeView.layer.cornerRadius = 5 badgeView.backgroundColor = UIColor.red let tabFrame = self.frame let percentX = (CGFloat(index) + 0.56) / CGFloat(itemCount) let x = (percentX * tabFrame.size.width).rounded(.up) let y = (CGFloat(0.1) * tabFrame.size.height).rounded(.up) badgeView.frame = CGRect(x: x, y: y, width: 10, height: 10) addSubview(badgeView) } //return true if removed success. @discardableResult public func removeItemBadge(atIndex index: Int) -> Bool { for subView in self.subviews { if subView.tag == (tabBarItemTag + index) { subView.removeFromSuperview() return true } } return false } }
vnjpjtjt2#
试试这个:
func repositionBadge(tabIndex: Int){ for badgeView in self.tabBarController!.tabBar.subviews[tabIndex].subviews { if NSStringFromClass(badgeView.classForCoder) == "_UIBadgeView" { badgeView.layer.transform = CATransform3DIdentity badgeView.layer.transform = CATransform3DMakeTranslation(-17.0, 1.0, 1.0) } } }
注意,tabIndex从1开始
8ulbf1ek3#
基于Marco Santarossa的answer,我将其更新为UITabBarController扩展,并进行了一些额外的调整:
UITabBarController
extension UITabBarController { func repositionBadgeLayer(_ badgeView: UIView) { if NSStringFromClass(badgeView.classForCoder) == "_UIBadgeView" { badgeView.layer.transform = CATransform3DIdentity badgeView.layer.transform = CATransform3DMakeTranslation(-17.0, 1.0, 1.0) } } func repositionBadges(tab: Int? = nil) { if let tabIndex = tab { for badgeView in self.tabBar.subviews[tabIndex].subviews { repositionBadgeLayer(badgeView) } } else { for tabBarSubviews in self.tabBar.subviews { for badgeView in tabBarSubviews.subviews { repositionBadgeLayer(badgeView) } } } } }
现在你可以在你的自定义UITabBarController类上使用它,只需调用:
repositionBadges()
或
repositionBadges(0)
0
6yt4nkrj4#
我不确定转换徽章层是否可以孤立,因为它有一些缺陷:
我想到了另一个解决办法
if UIDevice.current.userInterfaceIdiom == .pad { self.standatartTabBarAppearance.inlineLayoutAppearance.normal.badgePositionAdjustment.horizontal = .init(16.0) self.standatartTabBarAppearance.stackedLayoutAppearance.normal.badgePositionAdjustment.horizontal = .init(16.0) self.standatartTabBarAppearance.compactInlineLayoutAppearance.normal.badgePositionAdjustment.horizontal = .init(16.0) } self.tabBar.standardAppearance = self.standatartTabBarAppearance
6psbrbz95#
iOS 13或更高版本时,可以通过将UITabBarItemAppearance对象分配给UITabBarAppearance对象的项目外观属性,并将其分配给tabBar控件的appearance属性来更改徽章位置。示例:
UITabBarItemAppearance
UITabBarAppearance
let itemAppearance = UITabBarItemAppearance() itemAppearance.normal.badgePositionAdjustment.horizontal = 10 let appearance = UITabBarAppearance() appearance.stackedLayoutAppearance = itemAppearance appearance.inlineLayoutAppearance = itemAppearance appearance.compactInlineLayoutAppearance = itemAppearance tabBar.standardAppearance = appearance
5条答案
按热度按时间a1o7rhls1#
下面是一个简单的红色斑点githubGist的例子。当然,如果你想在里面添加一个标签,你可以自定义你自己的
badgeView = UIView()
。Swift 4.2
vnjpjtjt2#
试试这个:
注意,tabIndex从1开始
8ulbf1ek3#
基于Marco Santarossa的answer,我将其更新为
UITabBarController
扩展,并进行了一些额外的调整:现在你可以在你的自定义
UITabBarController
类上使用它,只需调用:repositionBadges()
应用于选项卡栏中可用的每个选项卡栏项目或
repositionBadges(0)
,其中0
指选项卡栏项索引,用于选择性应用6yt4nkrj4#
我不确定转换徽章层是否可以孤立,因为它有一些缺陷:
我想到了另一个解决办法
6psbrbz95#
iOS 13或更高版本时,可以通过将
UITabBarItemAppearance
对象分配给UITabBarAppearance
对象的项目外观属性,并将其分配给tabBar控件的appearance属性来更改徽章位置。示例: