Skip to content

Commit

Permalink
[Banner] Add intrinsicContentSize support with an example. (#7620)
Browse files Browse the repository at this point in the history
Previously, `intrinsicContentSize` was not supported in MDCBannerView, which means users have to specify layout constraints for width and height manually. After adding `intrinsicContentSize` support, users will only need to specify width constraints for banner to show it properly. This implementation and behavior matches UITextView.

Screenshot:
![Simulator Screen Shot - iPhone 7 - 2019-06-17 at 17 18 30](https://user-images.githubusercontent.com/8836258/59637507-f3b32e80-9123-11e9-8133-7108c1a7824e.png)
  • Loading branch information
wenyuzhang666 committed Jun 25, 2019
1 parent cc5cce8 commit 37a495b
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
3 changes: 3 additions & 0 deletions components/Banner/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ mdc_examples_objc_library(
name = "ObjcExamples",
deps = [
":Banner",
"//components/Buttons",
"//components/Buttons:Theming",
"//components/schemes/Color",
"//components/schemes/Container",
],
)

Expand Down
101 changes: 101 additions & 0 deletions components/Banner/examples/BannerAutolayoutExampleViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 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 <UIKit/UIKit.h>

#import "MaterialBanner.h"
#import "MaterialButtons+Theming.h"
#import "MaterialButtons.h"
#import "MaterialColorScheme.h"
#import "MaterialContainerScheme.h"

static NSString *const exampleText = @"Lorem ipsum dolor";

@interface BannerAutolayoutExampleViewController : UIViewController

@property(nonatomic, readwrite, strong) MDCContainerScheme *containerScheme;
@property(nonatomic, readwrite, strong) MDCBannerView *bannerView;

@end

@implementation BannerAutolayoutExampleViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.containerScheme = [[MDCContainerScheme alloc] init];
self.view.backgroundColor = self.containerScheme.colorScheme.backgroundColor;

// Action Button
MDCButton *button = [[MDCButton alloc] init];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button applyTextThemeWithScheme:self.containerScheme];
[button setTitle:@"Material Banner" forState:UIControlStateNormal];
[button addTarget:self
action:@selector(didTapButton)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
NSLayoutConstraint *buttonConstraintCenterX =
[button.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor];
buttonConstraintCenterX.active = YES;
NSLayoutConstraint *buttonConstraintCenterY =
[button.centerYAnchor constraintEqualToAnchor:self.view.centerYAnchor];
buttonConstraintCenterY.active = YES;

// Prepare Banner
MDCBannerView *bannerView = [[MDCBannerView alloc] init];
bannerView.translatesAutoresizingMaskIntoConstraints = NO;
bannerView.textLabel.text = exampleText;
bannerView.trailingButton.hidden = YES;
bannerView.showsDivider = YES;
bannerView.layoutMargins = UIEdgeInsetsZero;
MDCButton *actionButton = bannerView.leadingButton;
[actionButton applyTextThemeWithScheme:self.containerScheme];
[actionButton setTitle:@"Dismiss" forState:UIControlStateNormal];
[actionButton addTarget:self
action:@selector(didTapDismissOnBannerView)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bannerView];
NSLayoutConstraint *bannerViewConstraintTop =
[bannerView.topAnchor constraintEqualToAnchor:self.view.layoutMarginsGuide.topAnchor];
bannerViewConstraintTop.active = YES;
NSLayoutConstraint *bannerViewConstraintLeft =
[bannerView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor];
bannerViewConstraintLeft.active = YES;
NSLayoutConstraint *bannerViewConstraintRight =
[bannerView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor];
bannerViewConstraintRight.active = YES;
bannerView.hidden = YES;
self.bannerView = bannerView;
}

- (void)didTapButton {
self.bannerView.hidden = NO;
}

- (void)didTapDismissOnBannerView {
self.bannerView.hidden = YES;
}

#pragma mark - CBC

+ (NSDictionary *)catalogMetadata {
return @{
@"breadcrumbs" : @[ @"Banner", @"Banner (Autolayout)" ],
@"primaryDemo" : @NO,
@"presentable" : @YES,
};
}

@end
10 changes: 10 additions & 0 deletions components/Banner/src/MDCBannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,23 @@ - (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake(size.width, frameHeight);
}

- (CGSize)intrinsicContentSize {
CGFloat intrinsicContentHeight = [self sizeThatFits:self.bounds.size].height;
return CGSizeMake(UIViewNoIntrinsicMetric, intrinsicContentHeight);
}

- (void)updateConstraints {
MDCBannerViewLayoutStyle layoutStyle = [self layoutStyleForSizeToFit:self.bounds.size];
[self updateConstraintsWithLayoutStyle:layoutStyle];

[super updateConstraints];
}

- (void)layoutSubviews {
[super layoutSubviews];
[self invalidateIntrinsicContentSize];
}

#pragma mark - Layout methods

- (void)updateConstraintsWithLayoutStyle:(MDCBannerViewLayoutStyle)layoutStyle {
Expand Down

0 comments on commit 37a495b

Please sign in to comment.