Skip to content

Commit 87d67d8

Browse files
committed
Add label as subview on UIActivityIndicatorView
1 parent cf68aa2 commit 87d67d8

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

WooCommerce/Classes/Extensions/UIButton+Helpers.swift

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ extension UIButton {
1010
func applyPrimaryButtonStyle() {
1111
var configuration = UIButton.Configuration.filled()
1212
configuration.contentInsets = .init(
13-
top: Style.defaultInsets.top,
14-
leading: Style.defaultInsets.leading,
15-
bottom: Style.defaultInsets.bottom,
16-
trailing: Style.defaultInsets.trailing
13+
top: Style.defaultVerticalInsets,
14+
leading: Style.defaultHorizontalInsets,
15+
bottom: Style.defaultVerticalInsets,
16+
trailing: Style.defaultHorizontalInsets
1717
)
1818
layer.borderColor = UIColor.primaryButtonBorder.cgColor
1919
layer.borderWidth = Style.defaultBorderWidth
@@ -47,10 +47,10 @@ extension UIButton {
4747
func applySecondaryButtonStyle() {
4848
var configuration = UIButton.Configuration.filled()
4949
configuration.contentInsets = .init(
50-
top: Style.defaultInsets.top,
51-
leading: Style.defaultInsets.leading,
52-
bottom: Style.defaultInsets.bottom,
53-
trailing: Style.defaultInsets.trailing
50+
top: Style.defaultVerticalInsets,
51+
leading: Style.defaultHorizontalInsets,
52+
bottom: Style.defaultVerticalInsets,
53+
trailing: Style.defaultHorizontalInsets
5454
)
5555
backgroundColor = .secondaryButtonBackground
5656
layer.borderColor = UIColor.secondaryButtonBorder.cgColor
@@ -85,10 +85,10 @@ extension UIButton {
8585
func applyLinkButtonStyle(enableMultipleLines: Bool = false) {
8686
var configuration = UIButton.Configuration.plain()
8787
configuration.contentInsets = .init(
88-
top: Style.defaultInsets.top,
89-
leading: Style.defaultInsets.leading,
90-
bottom: Style.defaultInsets.bottom,
91-
trailing: Style.defaultInsets.trailing
88+
top: Style.defaultVerticalInsets,
89+
leading: Style.defaultHorizontalInsets,
90+
bottom: Style.defaultVerticalInsets,
91+
trailing: Style.defaultHorizontalInsets
9292
)
9393
backgroundColor = .clear
9494
tintColor = .accent
@@ -113,8 +113,13 @@ extension UIButton {
113113
}
114114

115115
func applyPaymentsModalCancelButtonStyle() {
116-
configuration = UIButton.Configuration.plain()
117-
configuration?.contentInsets = Style.defaultInsets
116+
var configuration = UIButton.Configuration.plain()
117+
configuration.contentInsets = .init(
118+
top: Style.defaultVerticalInsets,
119+
leading: Style.defaultHorizontalInsets,
120+
bottom: Style.defaultVerticalInsets,
121+
trailing: Style.defaultHorizontalInsets
122+
)
118123
backgroundColor = .tertiarySystemBackground
119124
layer.borderColor = UIColor.secondaryButtonBorder.cgColor
120125
layer.borderWidth = Style.defaultBorderWidth
@@ -166,8 +171,16 @@ extension UIButton {
166171
private func enableMultipleLines() {
167172
titleLabel?.lineBreakMode = .byWordWrapping
168173
if let label = titleLabel {
169-
self.addSubview(label)
170-
pinSubviewToAllEdgeMargins(label)
174+
if self.subviews.contains(where: { $0 == label }) {
175+
pinSubviewToAllEdgeMargins(label)
176+
} else {
177+
DDLogWarn("""
178+
Failed attempt to pin button's title to the edges.
179+
This is likely because the custom button title label was not added to its view hierarchy
180+
See ButtonActivityIndicator as an example
181+
"""
182+
)
183+
}
171184
}
172185
}
173186
}
@@ -180,6 +193,7 @@ private extension UIButton {
180193
struct Style {
181194
static let defaultCornerRadius = CGFloat(8.0)
182195
static let defaultBorderWidth = CGFloat(1.0)
183-
static let defaultEdgeInsets = UIEdgeInsets(top: 12, left: 22, bottom: 12, right: 22)
196+
static let defaultVerticalInsets = CGFloat(12)
197+
static let defaultHorizontalInsets = CGFloat(22)
184198
}
185199
}

WooCommerce/Classes/ViewRelated/ReusableViews/ButtonActivityIndicator.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ final class ButtonActivityIndicator: UIButton {
66

77
private let indicator: UIActivityIndicatorView = UIActivityIndicatorView(style: .medium)
88

9+
// We're adding the UILabel as subview in order to avoid a crash when
10+
// using large system fonts for accessibility. Trying to adapt the label
11+
// to its container will fail as these do not share ancestor
12+
// https://github.com/woocommerce/woocommerce-ios/pull/8094
13+
override init(frame: CGRect) {
14+
super.init(frame: frame)
15+
if let titleLabel {
16+
addSubview(titleLabel)
17+
}
18+
}
19+
20+
required init?(coder: NSCoder) {
21+
super.init(coder: coder)
22+
if let titleLabel {
23+
addSubview(titleLabel)
24+
}
25+
}
26+
927
override func layoutSubviews() {
1028
super.layoutSubviews()
1129

0 commit comments

Comments
 (0)