From 55e19b55b39c782702ac6261b1e126332822b8a3 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 26 Apr 2018 07:46:14 -0400 Subject: [PATCH] Fix layout for iOS9/10 code cleanup // FREEBIE --- .../ConversationHeaderView.swift | 13 +- .../ConversationViewController.m | 118 ++++++------------ .../HomeView/HomeViewController.m | 5 +- 3 files changed, 42 insertions(+), 94 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationHeaderView.swift b/Signal/src/ViewControllers/ConversationView/ConversationHeaderView.swift index c94358c65..599326d57 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationHeaderView.swift +++ b/Signal/src/ViewControllers/ConversationView/ConversationHeaderView.swift @@ -20,9 +20,6 @@ public class ConversationHeaderView: UIStackView { } set { self.titleLabel.attributedText = newValue -// self.layoutIfNeeded() -// self.titleLabel.sizeToFit() -// self.sizeToFit() } } @@ -32,9 +29,6 @@ public class ConversationHeaderView: UIStackView { } set { self.subtitleLabel.attributedText = newValue -// self.layoutIfNeeded() -// self.subtitleLabel.sizeToFit() -// self.sizeToFit() } } @@ -51,8 +45,6 @@ public class ConversationHeaderView: UIStackView { public let titleSecondaryFont: UIFont = UIFont.ows_regularFont(withSize: 9) public let subtitleFont: UIFont = UIFont.ows_regularFont(withSize: 12) -// public let columns: UIStackView -// public let textRows: UIStackView private let titleLabel: UILabel private let subtitleLabel: UILabel private let avatarView: AvatarImageView @@ -90,9 +82,6 @@ public class ConversationHeaderView: UIStackView { self.layoutMargins = UIEdgeInsets(top: 4, left: 2, bottom: 4, right: 2) self.isLayoutMarginsRelativeArrangement = true - // needed for proper layout on iOS10 - self.translatesAutoresizingMaskIntoConstraints = false - self.axis = .horizontal self.alignment = .center self.spacing = 0 @@ -112,8 +101,8 @@ public class ConversationHeaderView: UIStackView { } public override var intrinsicContentSize: CGSize { - // Grow to fill as much of the navbar as possible. if #available(iOS 11, *) { + // Grow to fill as much of the navbar as possible. return UILayoutFittingExpandedSize } else { return super.intrinsicContentSize diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index e9c0a5cb7..7f39a6744 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -179,8 +179,6 @@ typedef enum : NSUInteger { @property (nonatomic, nullable) NSTimer *readTimer; @property (nonatomic) NSCache *cellMediaCache; @property (nonatomic) ConversationHeaderView *headerView; -//@property (nonatomic) UILabel *navigationBarTitleLabel; -@property (nonatomic) UILabel *navigationBarSubtitleLabel; @property (nonatomic, nullable) UIView *bannerView; @property (nonatomic, nullable) OWSDisappearingMessagesConfiguration *disappearingMessagesConfiguration; @@ -490,7 +488,7 @@ typedef enum : NSUInteger { [self createConversationScrollButtons]; [self createHeaderViews]; - // [self createBackButton]; + [self addNotificationListeners]; [self loadDraftInCompose]; } @@ -1106,35 +1104,26 @@ typedef enum : NSUInteger { } self.headerView.attributedTitle = name; - // self.navigationBarTitleLabel.attributedText = name; - - // TODO layout still necessary with stackview? - // Changing the title requires relayout of the nav bar contents. - [self updateBarButtonItems]; } - (void)createHeaderViews { - // _backButtonUnreadCountView = [UIView new]; - // _backButtonUnreadCountView.layer.cornerRadius = self.unreadCountViewDiameter / 2; - // _backButtonUnreadCountView.backgroundColor = [UIColor redColor]; - // _backButtonUnreadCountView.hidden = YES; - // _backButtonUnreadCountView.userInteractionEnabled = NO; - // - // _backButtonUnreadCountLabel = [UILabel new]; - // _backButtonUnreadCountLabel.backgroundColor = [UIColor clearColor]; - // _backButtonUnreadCountLabel.textColor = [UIColor whiteColor]; - // _backButtonUnreadCountLabel.font = [UIFont systemFontOfSize:11]; - // _backButtonUnreadCountLabel.textAlignment = NSTextAlignmentCenter; - // + _backButtonUnreadCountView = [UIView new]; + _backButtonUnreadCountView.layer.cornerRadius = self.unreadCountViewDiameter / 2; + _backButtonUnreadCountView.backgroundColor = [UIColor redColor]; + _backButtonUnreadCountView.hidden = YES; + _backButtonUnreadCountView.userInteractionEnabled = NO; + + _backButtonUnreadCountLabel = [UILabel new]; + _backButtonUnreadCountLabel.backgroundColor = [UIColor clearColor]; + _backButtonUnreadCountLabel.textColor = [UIColor whiteColor]; + _backButtonUnreadCountLabel.font = [UIFont systemFontOfSize:11]; + _backButtonUnreadCountLabel.textAlignment = NSTextAlignmentCenter; ConversationHeaderView *headerView = [[ConversationHeaderView alloc] initWithThread:self.thread contactsManager:self.contactsManager]; headerView.delegate = self; - // UIImage *avatarImage = [OWSAvatarBuilder buildImageForThread:self.thread diameter:36 - // contactsManager:self.contactsManager]; headerView.avatarImage = avatarImage; - self.headerView = headerView; #ifdef USE_DEBUG_UI @@ -1143,24 +1132,9 @@ typedef enum : NSUInteger { action:@selector(navigationTitleLongPressed:)]]; #endif - // self.navigationBarTitleLabel = [UILabel new]; - // self.navigationBarTitleView.titleLabel = self.navigationBarTitleLabel; - // self.navigationBarTitleLabel.textColor = [UIColor whiteColor]; - // self.navigationBarTitleLabel.font = [self navigationBarTitleLabelFont]; - // self.navigationBarTitleLabel.lineBreakMode = NSLineBreakByTruncatingTail; - // [self.navigationBarTitleView addSubview:self.navigationBarTitleLabel]; - - // self.navigationBarSubtitleLabel = [UILabel new]; - // self.navigationBarTitleView.subtitleLabel = self.navigationBarSubtitleLabel; [self updateNavigationBarSubtitleLabel]; - // [self.navigationBarTitleView addSubview:self.navigationBarSubtitleLabel]; } -//- (UIFont *)navigationBarTitleLabelFont -//{ -// return [UIFont ows_boldFontWithSize:20.f]; -//} - - (CGFloat)unreadCountViewDiameter { return 16; @@ -1199,35 +1173,17 @@ typedef enum : NSUInteger { - (void)updateBarButtonItems { if (self.navigationItem.titleView == nil) { - // Request "full width" title; the navigation bar will truncate this - // to fit between the left and right buttons. - // self.navigationBarTitleView.frame = CGRectMake(0, 0, screenWidth, 44); - DDLogDebug(@"%@ assigning titleView", self.logTag); - - UILabel *label1 = [UILabel new]; - label1.text = @"title"; - label1.textColor = UIColor.whiteColor; - label1.font = [UIFont ows_regularFontWithSize:17]; - UILabel *label2 = [UILabel new]; - label2.text = @"subtitle"; - label2.textColor = UIColor.whiteColor; - label2.font = [UIFont ows_regularFontWithSize:12]; - - UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:@[ label1, label2 ]]; - stackView.axis = UILayoutConstraintAxisVertical; - - [label1 setCompressionResistanceHigh]; - [label2 setCompressionResistanceHigh]; - [stackView setCompressionResistanceHigh]; - // [stackView autoSetDimensionsToSize:CGSizeMake(100, 40)]; - - // self.navigationItem.titleView = stackView; + if (@available(iOS 11, *)) { + // Do nothing, we use autolayout/intrinsic content size to grow + } else { + // Request "full width" title; the navigation bar will truncate this + // to fit between the left and right buttons. + CGSize screenSize = [UIScreen mainScreen].bounds.size; + CGRect headerFrame = CGRectMake(0, 0, screenSize.width, 44); + self.headerView.frame = headerFrame; + } + self.navigationItem.titleView = self.headerView; - } else { - // Don't reset the frame of the navigationBarTitleView every time - // this method is called or we'll gave bad frames where it will appear - // in the wrong position. - // [self.navigationBarTitleView layoutSubviews]; } if (self.userLeftGroup) { @@ -3819,21 +3775,21 @@ typedef enum : NSUInteger { - (void)setBackButtonUnreadCount:(NSUInteger)unreadCount { - // OWSAssertIsOnMainThread(); - // if (_backButtonUnreadCount == unreadCount) { - // // No need to re-render same count. - // return; - // } - // _backButtonUnreadCount = unreadCount; - // - // OWSAssert(_backButtonUnreadCountView != nil); - // _backButtonUnreadCountView.hidden = unreadCount <= 0; - // - // OWSAssert(_backButtonUnreadCountLabel != nil); - // - // // Max out the unread count at 99+. - // const NSUInteger kMaxUnreadCount = 99; - // _backButtonUnreadCountLabel.text = [OWSFormat formatInt:(int)MIN(kMaxUnreadCount, unreadCount)]; + OWSAssertIsOnMainThread(); + if (_backButtonUnreadCount == unreadCount) { + // No need to re-render same count. + return; + } + _backButtonUnreadCount = unreadCount; + + OWSAssert(_backButtonUnreadCountView != nil); + _backButtonUnreadCountView.hidden = unreadCount <= 0; + + OWSAssert(_backButtonUnreadCountLabel != nil); + + // Max out the unread count at 99+. + const NSUInteger kMaxUnreadCount = 99; + _backButtonUnreadCountLabel.text = [OWSFormat formatInt:(int)MIN(kMaxUnreadCount, unreadCount)]; } #pragma mark 3D Touch Preview Actions diff --git a/Signal/src/ViewControllers/HomeView/HomeViewController.m b/Signal/src/ViewControllers/HomeView/HomeViewController.m index d5dfe5ac5..af6b4774c 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewController.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewController.m @@ -279,8 +279,11 @@ NSString *const kArchivedConversationsReuseIdentifier = @"kArchivedConversations // Admittedly this is kind of a hack and not super fine grained, but it's simple and results in the interactive pop // gesture animating our title view nicely vs. creating our own back button bar item with custom padding, which does // not properly animate with the "swipe to go back" or "swipe left for info" gestures. + NSUInteger paddingLength = 3; + NSString *paddingString = [@"" stringByPaddingToLength:paddingLength withString:@" " startingAtIndex:0]; + self.navigationItem.backBarButtonItem = - [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:nil action:nil]; + [[UIBarButtonItem alloc] initWithTitle:paddingString style:UIBarButtonItemStylePlain target:nil action:nil]; if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] && (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)) {