Skip to content

Commit

Permalink
[Buttons] Move MDCButton's defaultContentInset configuration to later…
Browse files Browse the repository at this point in the history
… in init (#8069)

After digging in a bit, I've found that this is an issue with UIButton in iOS 13 (as of Xcode 11 beta 4). To reproduce:

1) Create a `UIButton`.
2) Create a `UIImage` and flip it using `imageWithHorizontallyFlippedOrientation`.
3) *Before setting the button's image*, give the button non-zero contentEdgeInsets (I used `{20, 10, 20, 10}`).
4) Set the image on the button.

You should be able to see that the image is not visible, and that the button's internal UIImageView's bounds.origin is `{ -(i.left + i.right), -(i.top + i.bottom) }` (where `i` is the button's contentEdgeInsets). In iOS 12, the button's imageView's bounds.origin is `{0, 0}`.


Closes #7983
  • Loading branch information
bryanoltman committed Jul 24, 2019
1 parent e58c550 commit 4fcf3cd
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
18 changes: 13 additions & 5 deletions components/Buttons/src/MDCButton.m
Expand Up @@ -167,11 +167,6 @@ - (void)commonMDCButtonInit {
self.adjustsImageWhenHighlighted = NO;
self.showsTouchWhenHighlighted = NO;

// Default content insets
self.contentEdgeInsets = [self defaultContentEdgeInsets];
_minimumSize = CGSizeZero;
_maximumSize = CGSizeZero;

self.layer.cornerRadius = MDCButtonDefaultCornerRadius;
if (!self.layer.shapeGenerator) {
self.layer.shadowPath = [self boundingPath].CGPath;
Expand Down Expand Up @@ -204,6 +199,19 @@ - (void)commonMDCButtonInit {
_rippleView = [[MDCStatefulRippleView alloc] initWithFrame:self.bounds];
_rippleView.rippleColor = [UIColor colorWithWhite:1 alpha:(CGFloat)0.12];

// Default content insets
// The default contentEdgeInsets are set here (instead of above, as they were previously) because
// of a UIButton bug introduced in the iOS 13 betas that is unresolved as of Xcode 11 beta 4
// (b/136088498) wherein setting self.contentEdgeInsets before accessing self.imageView causes the
// imageView's bounds.origin to be set to { -(i.left + i.right), -(i.top + i.bottom) }
// This causes images created by using imageWithHorizontallyFlippedOrientation to not display.
// Images that have not been created this way seem to be fine.
// This behavior can also be seen in vanilla UIButtons by setting contentEdgeInsets to non-zero
// inset values and then setting an image created with imageWithHorizontallyFlippedOrientation.
self.contentEdgeInsets = [self defaultContentEdgeInsets];
_minimumSize = CGSizeZero;
_maximumSize = CGSizeZero;

// Uppercase all titles
if (_uppercaseTitle) {
[self updateTitleCase];
Expand Down
@@ -0,0 +1,65 @@
// Copyright 2019-present the Material Components for iOS authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "MaterialSnapshot.h"

#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKit.h>

#import "MaterialButtons.h"

/** Snapshot tests for Buttons in different UIControlStates */
@interface ButtonsFlippedImageRenderingSnapshotTests : MDCSnapshotTestCase
@property(nonatomic, strong) MDCButton *button;
@end

@implementation ButtonsFlippedImageRenderingSnapshotTests

- (void)setUp {
[super setUp];
self.button = [[MDCButton alloc] init];
}

- (void)tearDown {
self.button = nil;
[super tearDown];
}

- (void)generateSnapshotAndVerifyForView:(UIView *)view {
[view sizeToFit];
UIView *snapshotView = [view mdc_addToBackgroundView];
[self snapshotVerifyView:snapshotView];
}

// This test exists to exercise a bug in iOS 13 (Xcode 11 betas 1-4) where setting contentEdgeInsets
// before a horizontally flipped UIImage causes the image to render outside of the UIImageView's
// frame.
- (void)testHorizontallyFlippedImageWithContentInsets {
// Given
if (@available(iOS 10.0, *)) {
// When
self.button.contentEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 20);
UIImage *testImage =
[[UIImage mdc_testImageOfSize:CGSizeMake(24, 24)] imageWithHorizontallyFlippedOrientation];
[self.button setImage:testImage forState:UIControlStateNormal];
} else {
// Fallback on earlier versions
}

// Then
[self.button sizeToFit];
[self snapshotVerifyViewForIOS13:self.button];
}

@end
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 4fcf3cd

Please sign in to comment.