Skip to content

Commit

Permalink
[Dialogs] Adding accessoryViewHorizontalInset API
Browse files Browse the repository at this point in the history
The API allows setting horizontal insets for the accessory view that are different from the message. This is most often used for dialogs that have both a message and an accessory view, as demonstrated in the attached example.

Additionally, this example demonstrates how to add an horizontal hairline as shown on the material.io spec for Confirmation Dialogs.

Note:
This new API is needed because clients do not always have control over the view that is being used as an accessory view, or that the view is used in multiple areas in the app, and cannot be customized.
Additionally, there's no way currently to set 0 insets for the accessory view, while still keeping the 24 value insets for the message, which is a relatively common scenario.
PiperOrigin-RevId: 334142026
  • Loading branch information
galiak11 authored and material-automation committed Sep 28, 2020
1 parent 89b43fd commit 9d9bd93
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class DialogsAccessoryExampleViewController: MDCCollectionViewController {
return alert
}

// Demonstrate a confirmation dialog with a custom table view.d
// Demonstrate a confirmation dialog with a custom table view.
func performConfirmationDialog() -> MDCAlertController {
let alert = MDCAlertController(title: "Phone ringtone", message: "Please select a ringtone:")
alert.addAction(MDCAlertAction(title: "OK", handler: handler))
Expand All @@ -153,6 +153,8 @@ class DialogsAccessoryExampleViewController: MDCCollectionViewController {
alertView.contentInsets.bottom = 0
// Decreasing vertical margin between the accessory view and the message
alertView.accessoryViewVerticalInset = 8
// Aligning the accessory view with the dialog's edge by removing all horizontal insets.
alertView.accessoryViewHorizontalInset = -alertView.contentInsets.left
}

alert.mdc_adjustsFontForContentSizeCategory = true // Enable dynamic type.
Expand Down Expand Up @@ -305,6 +307,7 @@ class ExampleTableSeparatorView: UIView, UITableViewDataSource {

func setup() {
tableView.dataSource = self
tableView.alwaysBounceVertical = false
addSubview(tableView)

let separator = UIView(frame: .zero)
Expand All @@ -315,7 +318,7 @@ class ExampleTableSeparatorView: UIView, UITableViewDataSource {
separator.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: self.topAnchor),
tableView.topAnchor.constraint(lessThanOrEqualTo: self.topAnchor),
tableView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
separator.topAnchor.constraint(equalTo: tableView.bottomAnchor),
Expand All @@ -341,7 +344,7 @@ class ExampleTableSeparatorView: UIView, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = ringtones[indexPath.row]
cell.indentationWidth = 0
cell.indentationLevel = 1
return cell
}
}
Expand Down
21 changes: 19 additions & 2 deletions components/Dialogs/src/MDCAlertControllerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
@property(nonatomic, assign) UIEdgeInsets titleInsets;

/**
The edge insets around the content view (which includes the message and/or the accessory view)
against the dialog edges or its neighbor elements, the title and the actions.
The edge insets around the content view (which includes the message, and the accessory view if it
is set) against the dialog edges or its neighbor elements, the title and the actions.
Default value is UIEdgeInsets(top: 24, leading: 24, bottom: 24, trailing: 24).
*/
Expand Down Expand Up @@ -105,6 +105,23 @@
*/
@property(nonatomic, assign) CGFloat accessoryViewVerticalInset;

/**
The horizontal inset that sets the leading and trailing margins of the accessory view. The accessory
insets are added to the contentInsets.leading and contentInsets.trailing values when calculating the
leading and trailing margins of the accessory view.
@note: Unlike the content's horizontal insets, the accessory insets use a single value for both
leading and trailing edge, while the final margins are calculated using both the content and
the accessory insets.
@note: You may use a negative `accessoryViewHorizontalInset` value in order to to decreases the
leading and trailing margins of the accessory view.
Default value is 0 (meaning, the accessory view's horizontal insets are determined soley by the
horizontal contentInsets values).
*/
@property(nonatomic, assign) CGFloat accessoryViewHorizontalInset;

@end

@interface MDCAlertControllerView (ToBeDeprecated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ - (instancetype)initWithFrame:(CGRect)frame {
self.actionsHorizontalMargin = 8.0f;
self.actionsVerticalMargin = 12.0f;
self.accessoryViewVerticalInset = 20.0f;
self.accessoryViewHorizontalInset = 0.0f;

self.titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[self addSubview:self.titleScrollView];
Expand Down Expand Up @@ -676,6 +677,9 @@ - (CGSize)calculatePreferredContentSizeForBounds:(CGSize)boundsSize {
// @param boundingWidth should not include any internal margins or padding
- (CGSize)calculateContentSizeThatFitsWidth:(CGFloat)boundingWidth {
CGFloat contentInsets = self.contentInsets.left + self.contentInsets.right;
if (self.accessoryViewHorizontalInset > 0) {
contentInsets += self.accessoryViewHorizontalInset * 2.0;
}
CGFloat titleInsets = self.titleInsets.left + self.titleInsets.right;
CGSize boundsSize = CGRectInfinite.size;

Expand Down Expand Up @@ -829,8 +833,9 @@ - (void)layoutSubviews {
CGRect titleFrame = [self titleFrameWithTitleSize:titleSize];
CGRect messageFrame = [self messageFrameWithSize:messageSize];
CGRect accessoryViewFrame = CGRectMake(
self.contentInsets.left, CGRectGetMaxY(messageFrame) + [self accessoryVerticalInset],
accessoryViewSize.width, accessoryViewSize.height);
self.contentInsets.left + self.accessoryViewHorizontalInset,
CGRectGetMaxY(messageFrame) + [self accessoryVerticalInset],
accessoryViewSize.width - self.accessoryViewHorizontalInset * 2.0, accessoryViewSize.height);

self.titleLabel.frame = titleFrame;
self.messageTextView.frame = messageFrame;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ - (void)testAlertContentAccessoryHaveCustomInsets {
// When
self.alertView.contentInsets = UIEdgeInsetsMake(10.f, 10.f, 10.f, 10.f);
self.alertView.accessoryViewVerticalInset = 0.f;
self.alertView.accessoryViewHorizontalInset = -5.0f;

// Then
[self generateSizedSnapshotAndVerifyForAlert:self.alertController];
Expand Down

0 comments on commit 9d9bd93

Please sign in to comment.