Skip to content

Commit

Permalink
PillButtonBar: Fixing warnings for iOS 15 target deployment version. (#…
Browse files Browse the repository at this point in the history
…1165)

* PillButtonBar: Fixing warnings for iOS 15 target deployment version.

* Adding availability checks for iOS 14.

* Removing extra empty lines.
  • Loading branch information
rdeassis committed Aug 17, 2022
1 parent 86f9938 commit 8d26fe7
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 44 deletions.
141 changes: 101 additions & 40 deletions ios/FluentUI/Pill Button Bar/PillButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 19 additions & 4 deletions ios/FluentUI/Pill Button Bar/PillButtonBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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()
}
}
Expand Down

0 comments on commit 8d26fe7

Please sign in to comment.