Skip to content

Commit

Permalink
[Banner] fix layout margin not being considered for all layout styles. (
Browse files Browse the repository at this point in the history
#7956)

closes https://github.com/material-components/material-components-ios/issues/7959

There was a misalignment issue  on the horizontal axis for non single line style banner. `widthLimit` should be used in all layout styles.
  • Loading branch information
wenyuzhang666 committed Jul 19, 2019
1 parent 5cdb7f9 commit f61504e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
26 changes: 15 additions & 11 deletions components/Banner/src/MDCBannerView.m
Expand Up @@ -327,15 +327,13 @@ - (void)deactivateAllConstraints {
- (CGSize)sizeThatFits:(CGSize)size {
MDCBannerViewLayoutStyle layoutStyle = [self layoutStyleForSizeToFit:size];
CGFloat frameHeight = 0.0f;
CGSize contentSize = [self contentSizeForLayoutSize:size];
switch (layoutStyle) {
case MDCBannerViewLayoutStyleSingleRow: {
frameHeight += kTopPaddingSmall + kBottomPadding;
CGFloat widthLimit = size.width;
CGFloat marginsPadding = self.layoutMargins.left + self.layoutMargins.right;
widthLimit -= marginsPadding;
widthLimit -= (kLeadingPadding + kTrailingPadding);
[self.leadingButton sizeToFit];
CGFloat buttonWidth = CGRectGetWidth(self.leadingButton.frame);
CGFloat widthLimit = contentSize.width;
widthLimit -= (buttonWidth + kHorizontalSpaceBetweenTextLabelAndButton);
if (!self.imageView.hidden) {
widthLimit -= kImageViewSideLength;
Expand All @@ -352,15 +350,15 @@ - (CGSize)sizeThatFits:(CGSize)size {
}
case MDCBannerViewLayoutStyleMultiRowAlignedButton: {
frameHeight += kTopPaddingLarge + kBottomPadding;
frameHeight += [self getFrameHeightOfImageViewAndTextLabelWithSizeToFit:size];
frameHeight += [self getFrameHeightOfImageViewAndTextLabelWithSizeToFit:contentSize];
CGSize leadingButtonSize = [self.leadingButton sizeThatFits:CGSizeZero];
CGSize trailingButtonSize = [self.trailingButton sizeThatFits:CGSizeZero];
frameHeight += MAX(leadingButtonSize.height, trailingButtonSize.height);
break;
}
case MDCBannerViewLayoutStyleMultiRowStackedButton: {
frameHeight += kTopPaddingLarge + kBottomPadding;
frameHeight += [self getFrameHeightOfImageViewAndTextLabelWithSizeToFit:size];
frameHeight += [self getFrameHeightOfImageViewAndTextLabelWithSizeToFit:contentSize];
CGSize leadingButtonSize = [self.leadingButton sizeThatFits:CGSizeZero];
CGSize trailingButtonSize = [self.trailingButton sizeThatFits:CGSizeZero];
frameHeight +=
Expand Down Expand Up @@ -460,10 +458,8 @@ - (MDCBannerViewLayoutStyle)layoutStyleForSizeToFit:(CGSize)sizeToFit {
}

MDCBannerViewLayoutStyle layoutStyle;
CGFloat remainingWidth = sizeToFit.width;
CGFloat marginsPadding = self.layoutMargins.left + self.layoutMargins.right;
remainingWidth -= marginsPadding;
remainingWidth -= (kLeadingPadding + kTrailingPadding);
CGSize contentSize = [self contentSizeForLayoutSize:sizeToFit];
CGFloat remainingWidth = contentSize.width;
[self.leadingButton sizeToFit];
if (self.trailingButton.hidden) {
CGFloat buttonWidth = CGRectGetWidth(self.leadingButton.frame);
Expand All @@ -487,7 +483,7 @@ - (MDCBannerViewLayoutStyle)layoutStyleForSizeToFit:(CGSize)sizeToFit {

- (CGFloat)getFrameHeightOfImageViewAndTextLabelWithSizeToFit:(CGSize)sizeToFit {
CGFloat frameHeight = 0;
CGFloat remainingWidth = sizeToFit.width - kLeadingPadding - kTrailingPadding;
CGFloat remainingWidth = sizeToFit.width;
CGSize textLabelSize = CGSizeZero;
if (!self.imageView.hidden) {
remainingWidth -= (kImageViewSideLength + kSpaceBetweenIconImageAndTextLabel);
Expand All @@ -501,6 +497,14 @@ - (CGFloat)getFrameHeightOfImageViewAndTextLabelWithSizeToFit:(CGSize)sizeToFit
return frameHeight;
}

- (CGSize)contentSizeForLayoutSize:(CGSize)layoutSize {
CGFloat remainingWidth = layoutSize.width;
CGFloat marginsPadding = self.layoutMargins.left + self.layoutMargins.right;
remainingWidth -= marginsPadding;
remainingWidth -= (kLeadingPadding + kTrailingPadding);
return CGSizeMake(remainingWidth, layoutSize.height);
}

- (CGFloat)widthSumForButtons:(NSArray<__kindof UIButton *> *)buttons {
CGFloat buttonsWidthSum = 0;
for (UIButton *button in buttons) {
Expand Down
31 changes: 31 additions & 0 deletions components/Banner/tests/snapshot/MDCBannerSnapshotTests.m
Expand Up @@ -281,6 +281,37 @@ - (void)testMiddleLengthTextWithLargeLayoutMargin {
[self generateSnapshotAndVerifyForView:self.bannerView];
}

- (void)testMiddleLengthTextAndStackedButtonsWithLargeLayoutMargin {
// Given
self.bannerView.textLabel.text = kBannerMiddleLengthText;
MDCButton *button1 = self.bannerView.leadingButton;
[button1 setTitle:@"Action1" forState:UIControlStateNormal];
[button1 setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
button1.uppercaseTitle = YES;
MDCButton *button2 = self.bannerView.trailingButton;
[button2 setTitle:@"Action2" forState:UIControlStateNormal];
[button2 setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
button2.uppercaseTitle = YES;
self.bannerView.bannerViewLayoutStyle = MDCBannerViewLayoutStyleMultiRowStackedButton;
self.bannerView.imageView.hidden = YES;

// When
if (@available(iOS 11.0, *)) {
NSDirectionalEdgeInsets directionalEdgeInsets = NSDirectionalEdgeInsetsZero;
directionalEdgeInsets.leading = kBannerLargeContentPadding;
directionalEdgeInsets.trailing = kBannerLargeContentPadding;
self.bannerView.directionalLayoutMargins = directionalEdgeInsets;
} else {
UIEdgeInsets margins = UIEdgeInsetsZero;
margins.left = kBannerLargeContentPadding;
margins.right = kBannerLargeContentPadding;
self.bannerView.layoutMargins = margins;
}

// Then
[self generateSnapshotAndVerifyForView:self.bannerView];
}

- (void)testSingleRowStyleLongTextWithSingleActionLTR {
// When
self.bannerView.textLabel.text = kBannerLongText;
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.

0 comments on commit f61504e

Please sign in to comment.