diff --git a/Demos/FluentUIDemo_iOS/FluentUI.Demo/Demos/NavigationControllerDemoController.swift b/Demos/FluentUIDemo_iOS/FluentUI.Demo/Demos/NavigationControllerDemoController.swift index 5bb364229..f0c274486 100644 --- a/Demos/FluentUIDemo_iOS/FluentUI.Demo/Demos/NavigationControllerDemoController.swift +++ b/Demos/FluentUIDemo_iOS/FluentUI.Demo/Demos/NavigationControllerDemoController.swift @@ -490,6 +490,7 @@ class RootViewController: UIViewController, UITableViewDataSource, UITableViewDe var showSearchProgressSpinner: Bool = true var showRainbowRingForAvatar: Bool = false var showBadgeOnBarButtonItem: Bool = false + var updateBadgeLabelStyle: Bool = false var allowsCellSelection: Bool = false { didSet { @@ -642,6 +643,20 @@ class RootViewController: UIViewController, UITableViewDataSource, UITableViewDe } if indexPath.row == 3 { + guard let cell = tableView.dequeueReusableCell(withIdentifier: BooleanCell.identifier, for: indexPath) as? BooleanCell else { + return UITableViewCell() + } + cell.setup(title: "Override Badge Label Style", + isOn: updateBadgeLabelStyle, + isSwitchEnabled: navigationItem.titleStyle == .largeLeading) + cell.titleNumberOfLines = 0 + cell.onValueChanged = { [weak self, weak cell] in + self?.updateBadgeStyle(isOn: cell?.isOn ?? false) + } + return cell + } + + if indexPath.row == 4 { guard let cell = tableView.dequeueReusableCell(withIdentifier: ActionsCell.identifier, for: indexPath) as? ActionsCell else { return UITableViewCell() } @@ -766,6 +781,12 @@ class RootViewController: UIViewController, UITableViewDataSource, UITableViewDe showBadgeOnBarButtonItem = isOn } + @objc private func updateBadgeStyle(isOn: Bool) { + let navigationBar = msfNavigationController?.msfNavigationBar + navigationBar?.overriddenBadgeLabelStyle = isOn ? NavigationBar.BadgeLabelStyleWrapper(style: .system) : nil + updateBadgeLabelStyle = isOn + } + @objc private func showTooltipButtonPressed() { let navigationBar = msfNavigationController?.msfNavigationBar guard let view = navigationBar?.barButtonItemView(with: BarButtonItemTag.threeDay.rawValue) else { diff --git a/Sources/FluentUI_iOS/Components/Navigation/NavigationBar.swift b/Sources/FluentUI_iOS/Components/Navigation/NavigationBar.swift index 21b0d01c6..1929ad3e2 100644 --- a/Sources/FluentUI_iOS/Components/Navigation/NavigationBar.swift +++ b/Sources/FluentUI_iOS/Components/Navigation/NavigationBar.swift @@ -126,6 +126,13 @@ open class NavigationBar: UINavigationBar, TokenizedControl, TwoLineTitleViewDel } } + @objc public class BadgeLabelStyleWrapper: NSObject { + public var style: BadgeLabelStyle + @objc public init(style: BadgeLabelStyle) { + self.style = style + } + } + @objc public static func navigationBarBackgroundColor(fluentTheme: FluentTheme?) -> UIColor { return backgroundColor(for: .system, theme: fluentTheme) } @@ -306,6 +313,15 @@ open class NavigationBar: UINavigationBar, TokenizedControl, TwoLineTitleViewDel // @objc dynamic - so we can do KVO on this @objc dynamic private(set) var style: Style = defaultStyle + // Override value for BadgeLabel Style. We can set to nil when we don't wanna override. + @objc public var overriddenBadgeLabelStyle: BadgeLabelStyleWrapper? { + didSet { + if let navigationItem = topItem { + updateBarButtonItems(with: navigationItem) + } + } + } + private var systemWantsCompactNavigationBar: Bool { return traitCollection.horizontalSizeClass == .compact && traitCollection.verticalSizeClass == .compact } @@ -735,7 +751,9 @@ open class NavigationBar: UINavigationBar, TokenizedControl, TwoLineTitleViewDel private func createBarButtonItemButton(with item: UIBarButtonItem, isLeftItem: Bool) -> UIButton { let button = BadgeLabelButton(type: .system) button.item = item - if style == .system { + if let badgeLabelStyle = overriddenBadgeLabelStyle?.style { + button.badgeLabelStyle = badgeLabelStyle + } else if style == .system { button.badgeLabelStyle = .system } else if style == .gradient { button.badgeLabelStyle = .brand