Skip to content

Commit

Permalink
[Dialogs] Consider all actions alignments for vertical layouts (#9804)
Browse files Browse the repository at this point in the history
Add leading, center, trailing and justified alignment to buttons in vertical layout (b/122654717)

Co-authored-by: Bryan Oltman <bryanoltman@gmail.com>
PiperOrigin-RevId: 297554381
  • Loading branch information
galiak11 and bryanoltman committed Feb 28, 2020
1 parent 0040f66 commit b32c232
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 24 deletions.
67 changes: 53 additions & 14 deletions components/Dialogs/src/private/MDCAlertControllerView+Private.m
Expand Up @@ -843,26 +843,65 @@ - (void)layoutHorizontalButtons:(NSArray<MDCButton *> *)buttons {
- (void)layoutVerticalButtons:(NSArray<MDCButton *> *)buttons {
UIEdgeInsets actionsInsets =
self.enableAdjustableInsets ? self.actionsInsets : MDCDialogActionsInsets;
CGPoint buttonCenter;
buttonCenter.x = self.actionsScrollView.contentSize.width / 2.0f;
buttonCenter.y = self.actionsScrollView.contentSize.height - actionsInsets.bottom;
CGFloat maxButtonWidth =
self.actionsScrollView.contentSize.width - (actionsInsets.left + actionsInsets.right);
for (UIButton *button in buttons) {
CGRect buttonRect = button.bounds;
CGFloat multiplier = self.orderVerticalActionsByEmphasis ? 1.f : -1.f;
CGPoint buttonCenter;
CGPoint buttonOrigin;
buttonOrigin.y = self.orderVerticalActionsByEmphasis
? actionsInsets.bottom
: self.actionsScrollView.contentSize.height - actionsInsets.bottom;
if (self.actionsHorizontalAlignmentInVerticalLayout == MDCContentHorizontalAlignmentCenter ||
self.actionsHorizontalAlignmentInVerticalLayout == MDCContentHorizontalAlignmentJustified) {
buttonCenter.x = self.actionsScrollView.contentSize.width / 2.0f;
buttonCenter.y = buttonOrigin.y;
for (UIButton *button in buttons) {
CGRect buttonRect = button.bounds;

if (CGRectGetWidth(buttonRect) > maxButtonWidth) {
buttonRect.size.width = maxButtonWidth;
button.bounds = buttonRect;
}
if (CGRectGetWidth(buttonRect) > maxButtonWidth ||
self.actionsHorizontalAlignmentInVerticalLayout ==
MDCContentHorizontalAlignmentJustified) {
buttonRect.size.width = maxButtonWidth;
button.bounds = buttonRect;
}

buttonCenter.y -= buttonRect.size.height / 2.0f;
buttonCenter.y += multiplier * (buttonRect.size.height / 2.0f);

button.center = buttonCenter;
button.center = buttonCenter;

if (button != buttons.lastObject) {
buttonCenter.y -= buttonRect.size.height / 2.0f;
buttonCenter.y -= MDCDialogActionsVerticalPadding;
if (button != buttons.lastObject) {
buttonCenter.y += multiplier * (buttonRect.size.height / 2.0f);
buttonCenter.y +=
multiplier * (self.enableAdjustableInsets ? self.actionsVerticalMargin
: MDCDialogActionsVerticalPadding);
}
}
} else {
// Leading/Trailing alignment.
for (UIButton *button in buttons) {
CGRect buttonRect = button.bounds;
buttonOrigin.x =
self.actionsHorizontalAlignmentInVerticalLayout == MDCContentHorizontalAlignmentTrailing
? self.actionsScrollView.contentSize.width - buttonRect.size.width -
actionsInsets.right
: actionsInsets.left;
buttonOrigin.y += multiplier * buttonRect.size.height;

buttonRect.origin = buttonOrigin;
button.frame = buttonRect;
if (button != buttons.lastObject) {
buttonOrigin.y +=
multiplier * (self.enableAdjustableInsets ? self.actionsVerticalMargin
: MDCDialogActionsVerticalPadding);
}
}
// Handle RTL
if (self.mdf_effectiveUserInterfaceLayoutDirection ==
UIUserInterfaceLayoutDirectionRightToLeft) {
for (UIButton *button in buttons) {
CGRect flippedRect = MDFRectFlippedHorizontally(button.frame, CGRectGetWidth(self.bounds));
button.frame = flippedRect;
}
}
}
}
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.

0 comments on commit b32c232

Please sign in to comment.