Skip to content

Commit

Permalink
[iOS] Refactor usage of UIButton.contentEdgeInsets Pt 4
Browse files Browse the repository at this point in the history
Background:
UIButton.contentEdgeInsets is deprecated as of iOS 15. As part of the
effort of bumping the min version of iOS to 15, we refactor the usage
of UIButton.contentEdgeInsets and replace it with
UIButton.configuration.contentInsets. A particular issue in creating
this CL was that documentation gives the idea that contentEdgeInsets,
titleEdgeInsets, and imageEdgeInsets should be replaced by
contentInsets. We are asking the CL reviewer to verify that this is
correct. Additionally, we use compile guards instead of @available
because we get deprecation warnings with @available. This may be because
@available is checked at runtime and these warnings happen during
compiling.

Bug: 1406561
Change-Id: I394cc39d44164b260bc4210d892691331395da85
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4284418
Commit-Queue: Joemer Ramos <joemerramos@chromium.org>
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1111905}
  • Loading branch information
Joemer Ramos authored and Chromium LUCI CQ committed Mar 1, 2023
1 parent c848cf6 commit ba4e832
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,21 @@ - (UIButton*)primaryActionButton {
_primaryActionButton.pointerInteractionEnabled = YES;
_primaryActionButton.pointerStyleProvider =
CreateOpaqueButtonPointerStyleProvider();
_primaryActionButton.titleEdgeInsets = UIEdgeInsetsMake(
0, kButtonHorizontalMargin, 0, kButtonHorizontalMargin);

// TODO(crbug.com/1418068): Simplify after minimum version required is >=
// iOS 15.
if (@available(iOS 15, *)) {
_primaryActionButton.configuration.contentInsets =
NSDirectionalEdgeInsetsMake(0, kButtonHorizontalMargin, 0,
kButtonHorizontalMargin);
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0
else {
_primaryActionButton.titleEdgeInsets = UIEdgeInsetsMake(
0, kButtonHorizontalMargin, 0, kButtonHorizontalMargin);
}
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0

[_primaryActionButton addTarget:self
action:@selector(didTapPrimaryActionButton)
forControlEvents:UIControlEventTouchUpInside];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,21 @@ - (UIButton*)createSecondaryActionButton {
forControlEvents:UIControlEventTouchUpInside];
[secondaryActionButton setTitle:self.secondaryActionString
forState:UIControlStateNormal];
secondaryActionButton.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);

// TODO(crbug.com/1418068): Simplify after minimum version required is >=
// iOS 15.
if (@available(iOS 15, *)) {
secondaryActionButton.configuration.contentInsets =
NSDirectionalEdgeInsetsMake(kButtonVerticalInsets, 0,
kButtonVerticalInsets, 0);
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0
else {
secondaryActionButton.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);
}
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0

[secondaryActionButton setBackgroundColor:[UIColor clearColor]];
UIColor* titleColor = [UIColor colorNamed:self.secondaryActionTextColor
? self.secondaryActionTextColor
Expand Down Expand Up @@ -726,8 +739,21 @@ - (UIButton*)createTertiaryButton {
forControlEvents:UIControlEventTouchUpInside];
[tertiaryActionButton setTitle:self.tertiaryActionString
forState:UIControlStateNormal];
tertiaryActionButton.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);

// TODO(crbug.com/1418068): Simplify after minimum version required is >=
// iOS 15.
if (@available(iOS 15, *)) {
tertiaryActionButton.configuration.contentInsets =
NSDirectionalEdgeInsetsMake(kButtonVerticalInsets, 0,
kButtonVerticalInsets, 0);
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0
else {
tertiaryActionButton.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);
}
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0

[tertiaryActionButton setBackgroundColor:[UIColor clearColor]];
UIColor* titleColor = [UIColor colorNamed:kBlueColor];
[tertiaryActionButton setTitleColor:titleColor forState:UIControlStateNormal];
Expand Down
16 changes: 14 additions & 2 deletions ios/chrome/common/ui/elements/form_input_accessory_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,20 @@ - (UIView*)viewForNavigationButtons {
[closeButton addTarget:self
action:@selector(closeButtonTapped)
forControlEvents:UIControlEventTouchUpInside];
closeButton.contentEdgeInsets = UIEdgeInsetsMake(
0, ManualFillCloseButtonLeftInset, 0, ManualFillCloseButtonRightInset);

// TODO(crbug.com/1418068): Simplify after minimum version required is >=
// iOS 15.
if (@available(iOS 15, *)) {
closeButton.configuration.contentInsets = NSDirectionalEdgeInsetsMake(
0, ManualFillCloseButtonLeftInset, 0, ManualFillCloseButtonRightInset);
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0
else {
closeButton.contentEdgeInsets = UIEdgeInsetsMake(
0, ManualFillCloseButtonLeftInset, 0, ManualFillCloseButtonRightInset);
}
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0

[closeButton setAccessibilityLabel:textData.closeButtonAccessibilityLabel];

self.nextButton = nextButton;
Expand Down
40 changes: 32 additions & 8 deletions ios/chrome/common/ui/promo_style/promo_style_view_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,23 @@ - (UIView*)specificContentView {
- (UIButton*)primaryActionButton {
if (!_primaryActionButton) {
_primaryActionButton = [[HighlightButton alloc] initWithFrame:CGRectZero];
_primaryActionButton.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);

// TODO(crbug.com/1418068): Simplify after minimum version required is >=
// iOS 15.
if (@available(iOS 15, *)) {
_primaryActionButton.configuration.contentInsets =
NSDirectionalEdgeInsetsMake(kButtonVerticalInsets, kMoreArrowMargin,
kButtonVerticalInsets, kMoreArrowMargin);
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0
else {
_primaryActionButton.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);
_primaryActionButton.titleEdgeInsets =
UIEdgeInsetsMake(0, kMoreArrowMargin, 0, kMoreArrowMargin);
}
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0

[_primaryActionButton setBackgroundColor:[UIColor colorNamed:kBlueColor]];
UIColor* titleColor = [UIColor colorNamed:kSolidButtonTextColor];
[_primaryActionButton setTitleColor:titleColor
Expand All @@ -603,8 +618,8 @@ - (UIButton*)primaryActionButton {

// Use `primaryActionString` even if scrolling to the end is mandatory
// because at the viewDidLoad stage, the scroll view hasn't computed its
// content height, so there is no way to knOow if scrolling is needed. This
// label will be updated at the viewDidAppear stage if necessary.
// content height, so there is no way to knOow if scrolling is needed.
// This label will be updated at the viewDidAppear stage if necessary.
[_primaryActionButton setTitle:self.primaryActionString
forState:UIControlStateNormal];
UILabel* titleLabel = _primaryActionButton.titleLabel;
Expand All @@ -613,8 +628,6 @@ - (UIButton*)primaryActionButton {
_primaryActionButton.titleLabel.adjustsFontForContentSizeCategory = YES;
_primaryActionButton.accessibilityIdentifier =
kPromoStylePrimaryActionAccessibilityIdentifier;
_primaryActionButton.titleEdgeInsets =
UIEdgeInsetsMake(0, kMoreArrowMargin, 0, kMoreArrowMargin);
_primaryActionButton.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
[_primaryActionButton addTarget:self
action:@selector(didTapPrimaryActionButton)
Expand Down Expand Up @@ -912,8 +925,19 @@ - (UIButton*)createButtonWithText:(NSString*)buttonText
accessibilityIdentifier:(NSString*)accessibilityIdentifier {
UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:buttonText forState:UIControlStateNormal];
button.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);

// TODO(crbug.com/1418068): Simplify after minimum version required is >=
// iOS 15.
if (@available(iOS 15, *)) {
button.configuration.contentInsets = NSDirectionalEdgeInsetsMake(
kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0
else {
button.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);
}
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0
[button setBackgroundColor:[UIColor clearColor]];
UIColor* titleColor = [UIColor colorNamed:kBlueColor];
[button setTitleColor:titleColor forState:UIControlStateNormal];
Expand Down
17 changes: 15 additions & 2 deletions ios/chrome/common/ui/util/button_util.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@

UIButton* PrimaryActionButton(BOOL pointer_interaction_enabled) {
UIButton* primary_blue_button = [UIButton buttonWithType:UIButtonTypeSystem];
primary_blue_button.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);

// TODO(crbug.com/1418068): Simplify after minimum version required is >=
// iOS 15.
if (@available(iOS 15, *)) {
primary_blue_button.configuration.contentInsets =
NSDirectionalEdgeInsetsMake(kButtonVerticalInsets, 0,
kButtonVerticalInsets, 0);
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0
else {
primary_blue_button.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);
}
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0

[primary_blue_button setBackgroundColor:[UIColor colorNamed:kBlueColor]];
UIColor* titleColor = [UIColor colorNamed:kSolidButtonTextColor];
[primary_blue_button setTitleColor:titleColor forState:UIControlStateNormal];
Expand Down
16 changes: 14 additions & 2 deletions ios/showcase/first_run/sc_first_run_hero_screen_view_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,20 @@ - (void)viewDidLoad {
- (UIButton*)createButton {
UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"Custom button" forState:UIControlStateNormal];
button.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);

// TODO(crbug.com/1418068): Simplify after minimum version required is >=
// iOS 15.
if (@available(iOS 15, *)) {
button.configuration.contentInsets = NSDirectionalEdgeInsetsMake(
kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0
else {
button.contentEdgeInsets =
UIEdgeInsetsMake(kButtonVerticalInsets, 0, kButtonVerticalInsets, 0);
}
#endif // __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_15_0

[button setBackgroundColor:[UIColor clearColor]];
UIColor* titleColor = [UIColor colorNamed:kBlueColor];
[button setTitleColor:titleColor forState:UIControlStateNormal];
Expand Down

0 comments on commit ba4e832

Please sign in to comment.