Skip to content

Commit

Permalink
Fixed the search bar deformation on iOS 11.2
Browse files Browse the repository at this point in the history
Fixed  the search bar deformation on iOS 11.2
  • Loading branch information
ko1o committed Dec 17, 2017
1 parent 52ef977 commit 5d5517e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions PYSearch/PYSearchViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ @interface PYSearchViewController () <UISearchBarDelegate, UITableViewDelegate,
*/
@property (nonatomic, assign) UIDeviceOrientation currentOrientation;

/**
The width of cancel button
*/
@property (nonatomic, assign) CGFloat cancelButtonWidth;

@end

@implementation PYSearchViewController
Expand Down Expand Up @@ -119,6 +124,7 @@ - (void)viewDidLayoutSubviews
}

UIButton *cancelButton = self.navigationItem.rightBarButtonItem.customView;
self.cancelButtonWidth = cancelButton.py_width > self.cancelButtonWidth ? cancelButton.py_width : self.cancelButtonWidth;
// Adapt the search bar layout problem in the navigation bar on iOS 11
// More details : https://github.com/iphone5solo/PYSearch/issues/108
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0) { // iOS 11
Expand All @@ -131,14 +137,14 @@ - (void)viewDidLayoutSubviews
break;
}
}
_searchBar.py_width = self.view.py_width - cancelButton.py_width - PYSEARCH_MARGIN * 3 - 8;
_searchBar.py_width = self.view.py_width - self.cancelButtonWidth - PYSEARCH_MARGIN * 3 - 8;
_searchBar.py_height = self.view.py_width > self.view.py_height ? 24 : 30;
_searchTextField.frame = _searchBar.bounds;
} else {
UIView *titleView = self.navigationItem.titleView;
titleView.py_x = PYSEARCH_MARGIN * 1.5;
titleView.py_y = self.view.py_width > self.view.py_height ? 3 : 7;
titleView.py_width = self.view.py_width - cancelButton.py_width - titleView.py_x * 2 - 3;
titleView.py_width = self.view.py_width - self.cancelButtonWidth - titleView.py_x * 2 - 3;
titleView.py_height = self.view.py_width > self.view.py_height ? 24 : 30;
}
}
Expand All @@ -159,6 +165,10 @@ - (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

if (self.cancelButtonWidth == 0) { // Just adapt iOS 11.2
[self viewDidLayoutSubviews];
}

// Adjust the view according to the `navigationBar.translucent`
if (NO == self.navigationController.navigationBar.translucent) {
self.baseSearchTableView.contentInset = UIEdgeInsetsMake(0, 0, self.view.py_y, 0);
Expand Down

0 comments on commit 5d5517e

Please sign in to comment.