From 8d26fe7949e49da37dc4e66b18d88390e85ec5bb Mon Sep 17 00:00:00 2001 From: rdeassis <68076145+rdeassis@users.noreply.github.com> Date: Wed, 17 Aug 2022 11:05:53 -0700 Subject: [PATCH] PillButtonBar: Fixing warnings for iOS 15 target deployment version. (#1165) * PillButtonBar: Fixing warnings for iOS 15 target deployment version. * Adding availability checks for iOS 14. * Removing extra empty lines. --- ios/FluentUI/Pill Button Bar/PillButton.swift | 141 +++++++++++++----- .../Pill Button Bar/PillButtonBar.swift | 23 ++- 2 files changed, 120 insertions(+), 44 deletions(-) diff --git a/ios/FluentUI/Pill Button Bar/PillButton.swift b/ios/FluentUI/Pill Button Bar/PillButton.swift index 54a1660054..b434f69309 100644 --- a/ios/FluentUI/Pill Button Bar/PillButton.swift +++ b/ios/FluentUI/Pill Button Bar/PillButton.swift @@ -51,7 +51,8 @@ open class PillButton: UIButton { updateAppearance() } - @objc public init(pillBarItem: PillButtonBarItem, style: PillButtonStyle = .primary) { + @objc public init(pillBarItem: PillButtonBarItem, + style: PillButtonStyle = .primary) { self.pillBarItem = pillBarItem self.style = style super.init(frame: .zero) @@ -105,20 +106,34 @@ open class PillButton: UIButton { } private func setupView() { - setTitle(pillBarItem.title, for: .normal) - titleLabel?.font = Constants.font + if #available(iOS 15.0, *) { + var configuration = UIButton.Configuration.plain() + configuration.attributedTitle = AttributedString(pillBarItem.title) + configuration.contentInsets = NSDirectionalEdgeInsets(top: Constants.topInset, + leading: Constants.horizontalInset, + bottom: Constants.bottomInset, + trailing: Constants.horizontalInset) + self.configuration = configuration + + configurationUpdateHandler = { [weak self] _ in + self?.updateAppearance() + } + } else { + setTitle(pillBarItem.title, for: .normal) + titleLabel?.font = Constants.font + + contentEdgeInsets = UIEdgeInsets(top: Constants.topInset, + left: Constants.horizontalInset, + bottom: Constants.bottomInset, + right: Constants.horizontalInset) + } + layer.cornerRadius = PillButton.cornerRadius clipsToBounds = true layer.cornerCurve = .continuous largeContentTitle = titleLabel?.text showsLargeContentViewer = true - - contentEdgeInsets = UIEdgeInsets(top: Constants.topInset, - left: Constants.horizontalInset, - bottom: Constants.bottomInset, - right: Constants.horizontalInset) - } private func updateAccessibilityTraits() { @@ -175,48 +190,94 @@ open class PillButton: UIButton { } private func updateAppearance() { - if let window = window { - if isSelected { - if isEnabled { - if let customSelectedBackgroundColor = customSelectedBackgroundColor { - backgroundColor = customSelectedBackgroundColor - } else { - backgroundColor = isHighlighted - ? PillButton.selectedHighlightedBackgroundColor(for: window, for: style) - : PillButton.selectedBackgroundColor(for: window, for: style) - } - - setTitleColor(customSelectedTextColor ?? PillButton.selectedTitleColor(for: window, for: style), for: .normal) - setTitleColor(customSelectedTextColor ?? PillButton.selectedHighlightedTitleColor(for: window, for: style), for: .highlighted) + guard let window = window else { + return + } + + // TODO: Once iOS 14 support is dropped, these should be converted to constants (let) that will be initialized by the logic below. + var resolvedBackgroundColor: UIColor = .clear + var resolvedTitleColor: UIColor = .clear + + if isSelected { + if isEnabled { + resolvedBackgroundColor = customSelectedBackgroundColor ?? (isHighlighted + ? PillButton.selectedHighlightedBackgroundColor(for: window, for: style) + : PillButton.selectedBackgroundColor(for: window, for: style)) + if #available(iOS 15.0, *) { + resolvedTitleColor = customSelectedTextColor ?? (isHighlighted ? PillButton.selectedHighlightedTitleColor(for: window, + for: style) + : PillButton.selectedTitleColor(for: window, + for: style)) } else { - backgroundColor = PillButton.selectedDisabledBackgroundColor(for: window, for: style) - setTitleColor(PillButton.selectedDisabledTitleColor(for: window, for: style), for: .normal) + setTitleColor(customSelectedTextColor ?? PillButton.selectedTitleColor(for: window, + for: style), + for: .normal) + setTitleColor(customSelectedTextColor ?? PillButton.selectedHighlightedTitleColor(for: window, + for: style), + for: .highlighted) } } else { - if let customBackgroundColor = customBackgroundColor { - backgroundColor = customBackgroundColor + resolvedBackgroundColor = PillButton.selectedDisabledBackgroundColor(for: window, + for: style) + if #available(iOS 15.0, *) { + resolvedTitleColor = PillButton.selectedDisabledTitleColor(for: window, + for: style) } else { - backgroundColor = isEnabled - ? (isHighlighted - ? PillButton.highlightedBackgroundColor(for: window, for: style) - : PillButton.normalBackgroundColor(for: window, for: style)) - : PillButton.disabledBackgroundColor(for: window, for: style) + setTitleColor(PillButton.selectedDisabledTitleColor(for: window, + for: style), + for: .normal) } - - if isEnabled { - setTitleColor(customTextColor ?? PillButton.titleColor(for: style), for: .normal) - setTitleColor(customTextColor ?? PillButton.highlightedTitleColor(for: window, for: style), for: .highlighted) + } + } else { + if isEnabled { + unreadDotColor = customUnreadDotColor ?? PillButton.enabledUnreadDotColor(for: window, + for: style) + resolvedBackgroundColor = customBackgroundColor ?? (isHighlighted + ? PillButton.highlightedBackgroundColor(for: window, + for: style) + : PillButton.normalBackgroundColor(for: window, + for: style)) + if #available(iOS 15.0, *) { + resolvedTitleColor = { + guard let customTextColor = customTextColor else { + if isHighlighted { + return PillButton.highlightedTitleColor(for: window, + for: style) + } + + return PillButton.titleColor(for: style) + } + + return customTextColor + }() } else { - setTitleColor(PillButton.disabledTitleColor(for: window, for: style), for: .disabled) + setTitleColor(customTextColor ?? PillButton.titleColor(for: style), + for: .normal) + setTitleColor(customTextColor ?? PillButton.highlightedTitleColor(for: window, + for: style), + for: .highlighted) } - - if isEnabled { - unreadDotColor = customUnreadDotColor ?? PillButton.enabledUnreadDotColor(for: window, for: style) + } else { + unreadDotColor = customUnreadDotColor ?? PillButton.disabledUnreadDotColor(for: window, + for: style) + resolvedBackgroundColor = customBackgroundColor ?? PillButton.disabledBackgroundColor(for: window, + for: style) + if #available(iOS 15.0, *) { + resolvedTitleColor = PillButton.disabledTitleColor(for: window, + for: style) } else { - unreadDotColor = customUnreadDotColor ?? PillButton.disabledUnreadDotColor(for: window, for: style) + setTitleColor(PillButton.disabledTitleColor(for: window, for: style), for: .disabled) } } } + + if #available(iOS 15.0, *) { + configuration?.background.backgroundColor = resolvedBackgroundColor + configuration?.attributedTitle?.setAttributes(AttributeContainer([NSAttributedString.Key.foregroundColor: resolvedTitleColor, + NSAttributedString.Key.font: Constants.font])) + } else { + backgroundColor = resolvedBackgroundColor + } } private struct Constants { diff --git a/ios/FluentUI/Pill Button Bar/PillButtonBar.swift b/ios/FluentUI/Pill Button Bar/PillButtonBar.swift index f3855be3e2..3dee4d64a9 100644 --- a/ios/FluentUI/Pill Button Bar/PillButtonBar.swift +++ b/ios/FluentUI/Pill Button Bar/PillButtonBar.swift @@ -125,6 +125,7 @@ open class PillButtonBar: UIScrollView { private var stackView: UIStackView = { let view = UIStackView() + view.distribution = .fillProportionally view.alignment = .center view.spacing = Constants.minButtonsSpacing return view @@ -411,8 +412,15 @@ open class PillButtonBar: UIScrollView { buttonExtraSidePadding = ceil(totalPadding / CGFloat(buttonEdges)) for button in buttons { button.layoutIfNeeded() - button.contentEdgeInsets.right += buttonExtraSidePadding - button.contentEdgeInsets.left += buttonExtraSidePadding + + if #available(iOS 15.0, *) { + button.configuration?.contentInsets.leading += buttonExtraSidePadding + button.configuration?.contentInsets.trailing += buttonExtraSidePadding + } else { + button.contentEdgeInsets.right += buttonExtraSidePadding + button.contentEdgeInsets.left += buttonExtraSidePadding + } + button.layoutIfNeeded() } } @@ -453,8 +461,15 @@ open class PillButtonBar: UIScrollView { let buttonWidth = button.frame.width if buttonWidth > 0, buttonWidth < Constants.minButtonWidth { let extraInset = floor((Constants.minButtonWidth - button.frame.width) / 2) - button.contentEdgeInsets.left += extraInset - button.contentEdgeInsets.right = button.contentEdgeInsets.left + + if #available(iOS 15.0, *) { + button.configuration?.contentInsets.leading += extraInset + button.configuration?.contentInsets.trailing = button.configuration?.contentInsets.leading ?? extraInset + } else { + button.contentEdgeInsets.left += extraInset + button.contentEdgeInsets.right = button.contentEdgeInsets.left + } + button.layoutIfNeeded() } }