Skip to content

Commit

Permalink
[Dialogs] Consider all actions alignments for horizontal layout (#9805)
Browse files Browse the repository at this point in the history
Adding center, leading, trailing and justified alignment to alert actions in horizontal layout.

Co-authored-by: Bryan Oltman <bryanoltman@gmail.com>
PiperOrigin-RevId: 297555954
  • Loading branch information
galiak11 and bryanoltman committed Feb 28, 2020
1 parent b32c232 commit 594e477
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 17 deletions.
40 changes: 35 additions & 5 deletions components/Dialogs/src/private/MDCAlertControllerView+Private.m
Expand Up @@ -762,7 +762,7 @@ - (void)layoutSubviews {
if (self.isVerticalActionsLayout) {
[self layoutVerticalButtons:buttons];
} else {
[self layoutHorizontalButtons:buttons];
[self layoutHorizontalButtons:buttons actionSize:actionSize];
}

// Place scrollviews
Expand Down Expand Up @@ -813,22 +813,52 @@ - (void)setHitAreaForButtons:(NSArray<MDCButton *> *)buttons {
}
}

- (void)layoutHorizontalButtons:(NSArray<MDCButton *> *)buttons {
- (void)layoutHorizontalButtons:(NSArray<MDCButton *> *)buttons actionSize:(CGSize)actionSize {
UIEdgeInsets actionsInsets =
self.enableAdjustableInsets ? self.actionsInsets : MDCDialogActionsInsets;
CGFloat horizontalMargin = (self.enableAdjustableInsets ? self.actionsHorizontalMargin
: MDCDialogActionsHorizontalPadding);
CGFloat maxButtonWidth =
self.actionsScrollView.contentSize.width - (actionsInsets.left + actionsInsets.right);
CGPoint buttonOrigin = CGPointZero;
buttonOrigin.x = self.actionsScrollView.contentSize.width - actionsInsets.right;
CGFloat buttonWidth = 0.f;
CGFloat multiplier = 1.f;
if (self.actionsHorizontalAlignment == MDCContentHorizontalAlignmentTrailing) {
buttonOrigin.x = self.actionsScrollView.contentSize.width - actionsInsets.right;
multiplier = -1.f;
} else if (self.actionsHorizontalAlignment == MDCContentHorizontalAlignmentCenter) {
CGFloat actionWidthNoInsets = actionSize.width - actionsInsets.left - actionsInsets.right;
buttonOrigin.x = (self.actionsScrollView.contentSize.width - actionWidthNoInsets) / 2.f;
} else { // leading or fill
buttonOrigin.x = actionsInsets.left;
}
buttonOrigin.y = actionsInsets.top;
for (UIButton *button in buttons) {
CGRect buttonRect = button.frame;

buttonOrigin.x -= buttonRect.size.width;
buttonWidth = buttonRect.size.width;

if (self.actionsHorizontalAlignment == MDCContentHorizontalAlignmentTrailing) {
buttonOrigin.x -= buttonRect.size.width;
} else if (self.actionsHorizontalAlignment == MDCContentHorizontalAlignmentJustified) {
if (buttons.count > 1) {
CGFloat totalMargin = horizontalMargin * (buttons.count - 1);
buttonWidth = (maxButtonWidth - totalMargin) / buttons.count;
} else {
buttonWidth = maxButtonWidth;
}
}
buttonRect.origin = buttonOrigin;
buttonRect.size.width = buttonWidth;

button.frame = buttonRect;

if (button != buttons.lastObject) {
buttonOrigin.x -= MDCDialogActionsHorizontalPadding;
if (self.actionsHorizontalAlignment != MDCContentHorizontalAlignmentTrailing) {
buttonOrigin.x += buttonRect.size.width + horizontalMargin;
} else {
buttonOrigin.x -= horizontalMargin;
}
}
}
// Handle RTL
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 594e477

Please sign in to comment.