From 7fb92c9ce3939701a40d922d1250d3abfb1a337c Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 6 May 2020 19:18:47 -0400 Subject: [PATCH] Prevent avatars in action notification group cell from overflowing --- ...ActionNotificationGroupTableViewCell.swift | 19 ++++++++++++++----- .../ActionNotificationGroupTableViewCell.xib | 15 ++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift b/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift index c34650ad..8805e31a 100644 --- a/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift +++ b/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.swift @@ -16,6 +16,7 @@ class ActionNotificationGroupTableViewCell: UITableViewCell { var mastodonController: MastodonController! { delegate?.apiController } @IBOutlet weak var actionImageView: UIImageView! + @IBOutlet weak var verticalStackView: UIStackView! @IBOutlet weak var actionAvatarStackView: UIStackView! @IBOutlet weak var timestampLabel: UILabel! @IBOutlet weak var actionLabel: UILabel! @@ -70,6 +71,7 @@ class ActionNotificationGroupTableViewCell: UITableViewCell { let people = group.notificationIDs.compactMap(mastodonController.cache.notification(for:)).map { $0.account } actionAvatarStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } + var imageViews = [UIImageView]() for account in people { let imageView = UIImageView() imageView.translatesAutoresizingMaskIntoConstraints = false @@ -83,12 +85,19 @@ class ActionNotificationGroupTableViewCell: UITableViewCell { } } actionAvatarStackView.addArrangedSubview(imageView) - NSLayoutConstraint.activate([ - imageView.widthAnchor.constraint(equalToConstant: 30), - imageView.heightAnchor.constraint(equalToConstant: 30) - ]) + imageViews.append(imageView) + + // don't add more avatars if they would overflow or squeeze the timestamp label + let avatarViewsWidth = 30 * CGFloat(imageViews.count) + let avatarMarginsWidth = 4 * CGFloat(max(0, imageViews.count - 1)) + let maxAvatarStackWidth = verticalStackView.bounds.width - timestampLabel.bounds.width - 8 + let remainingWidth = maxAvatarStackWidth - avatarViewsWidth - avatarMarginsWidth + if remainingWidth < 34 { + break + } } - + NSLayoutConstraint.activate(imageViews.map { $0.widthAnchor.constraint(equalTo: $0.heightAnchor) }) + updateTimestamp() updateActionLabel(people: people) diff --git a/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.xib b/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.xib index b9c06cdb..cc42d57c 100644 --- a/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.xib +++ b/Tusker/Views/Notifications/ActionNotificationGroupTableViewCell.xib @@ -1,8 +1,8 @@ - + - + @@ -19,19 +19,19 @@ - + - - + + - + -