From 0ccddb696aaed44360bb394a008d4c1c6ae433bd Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 13 Oct 2017 12:57:44 -0400 Subject: [PATCH] Add workaround for bug in iOS 11.1 beta around hit area of custom back buttons. // FREEBIE --- .../ConversationViewController.m | 44 ++++++++++--------- Signal/src/util/UIViewController+OWS.m | 16 +++++++ 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index bfdb3ae44..bc4acffa5 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -1106,27 +1106,29 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { - (void)createBackButton { UIBarButtonItem *backItem = [self createOWSBackButton]; - // This method gets called multiple times, so it's important we re-layout the unread badge - // with respect to the new backItem. - [backItem.customView addSubview:_backButtonUnreadCountView]; - // TODO: The back button assets are assymetrical. There are strong reasons - // to use spacing in the assets to manipulate the size and positioning of - // bar button items, but it means we'll probably need separate RTL and LTR - // flavors of these assets. - [_backButtonUnreadCountView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:-6]; - [_backButtonUnreadCountView autoPinLeadingToSuperviewWithMargin:1]; - [_backButtonUnreadCountView autoSetDimension:ALDimensionHeight toSize:self.unreadCountViewDiameter]; - // We set a min width, but we will also pin to our subview label, so we can grow to accommodate multiple digits. - [_backButtonUnreadCountView autoSetDimension:ALDimensionWidth - toSize:self.unreadCountViewDiameter - relation:NSLayoutRelationGreaterThanOrEqual]; - - [_backButtonUnreadCountView addSubview:_backButtonUnreadCountLabel]; - [_backButtonUnreadCountLabel autoPinWidthToSuperviewWithMargin:4]; - [_backButtonUnreadCountLabel autoPinHeightToSuperview]; - - // Initialize newly created unread count badge to accurately reflect the current unread count. - [self updateBackButtonUnreadCount]; + if (backItem.customView) { + // This method gets called multiple times, so it's important we re-layout the unread badge + // with respect to the new backItem. + [backItem.customView addSubview:_backButtonUnreadCountView]; + // TODO: The back button assets are assymetrical. There are strong reasons + // to use spacing in the assets to manipulate the size and positioning of + // bar button items, but it means we'll probably need separate RTL and LTR + // flavors of these assets. + [_backButtonUnreadCountView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:-6]; + [_backButtonUnreadCountView autoPinLeadingToSuperviewWithMargin:1]; + [_backButtonUnreadCountView autoSetDimension:ALDimensionHeight toSize:self.unreadCountViewDiameter]; + // We set a min width, but we will also pin to our subview label, so we can grow to accommodate multiple digits. + [_backButtonUnreadCountView autoSetDimension:ALDimensionWidth + toSize:self.unreadCountViewDiameter + relation:NSLayoutRelationGreaterThanOrEqual]; + + [_backButtonUnreadCountView addSubview:_backButtonUnreadCountLabel]; + [_backButtonUnreadCountLabel autoPinWidthToSuperviewWithMargin:4]; + [_backButtonUnreadCountLabel autoPinHeightToSuperview]; + + // Initialize newly created unread count badge to accurately reflect the current unread count. + [self updateBackButtonUnreadCount]; + } self.navigationItem.leftBarButtonItem = backItem; } diff --git a/Signal/src/util/UIViewController+OWS.m b/Signal/src/util/UIViewController+OWS.m index 3fb84e5c7..726654544 100644 --- a/Signal/src/util/UIViewController+OWS.m +++ b/Signal/src/util/UIViewController+OWS.m @@ -53,6 +53,22 @@ NS_ASSUME_NONNULL_BEGIN = CGRectMake(0, 0, backImage.size.width + kExtraRightPadding, backImage.size.height + kExtraHeightPadding); backButton.frame = buttonFrame; + if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(11, 1)) { + // In iOS 11.1 beta, the hot area of custom bar button items is _only_ + // the bounds of the custom view, making them very hard to hit. + // + // TODO: Remove this hack if the bug is fixed in iOS 11.1 by the time + // it goes to production (or in a later release), + // since it has two negative side effects: 1) the layout of the + // back button isn't consistent with the iOS default back buttons + // 2) we can't add the unread count badge to the back button + // with this hack. + return [[UIBarButtonItem alloc] initWithImage:backImage + style:UIBarButtonItemStylePlain + target:target + action:selector]; + } + UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; backItem.width = buttonFrame.size.width;