Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios) : Added functionality to show search bar in navigation bar for TiUIListView and TiUITableView #10664

Merged
merged 10 commits into from
Mar 7, 2019
13 changes: 13 additions & 0 deletions apidoc/Titanium/UI/ListView.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,19 @@ properties:
platforms: [iphone, ipad]
availability: creation

- name: showSearchBarInNavBar
summary: A Boolean indicating whether search bar will be in navigation bar.
description: |
If you want to show the search bar in navigation bar, set this property `true` during creation.
Use the <Titanium.UI.Window.hidesSearchBarWhenScrolling> property to control the visibility of the
searchbar when scrolling.
type: Boolean
default: false
since: 8.1.0
platforms: [iphone, ipad]
osver: {ios: {min: "11.0"}}
availability: creation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

osver: {ios: {min: "11.0"}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks!


- name: resultsBackgroundColor
summary: The background color of the search results (iOS-only).
description: |
Expand Down
13 changes: 13 additions & 0 deletions apidoc/Titanium/UI/TableView.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1350,6 +1350,19 @@ properties:
platforms: [iphone, ipad]
availability: creation

- name: showSearchBarInNavBar
summary: A Boolean indicating whether search bar will be in navigation bar.
description: |
If you want to show the search bar in navigation bar, set this property `true` during creation.
Use the <Titanium.UI.Window.hidesSearchBarWhenScrolling> property to control the visibility of the
searchbar when scrolling.
type: Boolean
default: false
since: 8.1.0
platforms: [iphone, ipad]
osver: {ios: {min: "11.0"}}
availability: creation

- name: searchAsChild
summary: Determines whether the [SearchBar](Titanium.UI.SearchBar) or [SearchView](Titanium.UI.Android.SearchView) appears as part of the TableView.
description: Set to false if the search view will be displayed in the action bar.
Expand Down
17 changes: 16 additions & 1 deletion apidoc/Titanium/UI/Window.yml
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,26 @@ properties:
title view when requested by the current navigation item. To specify when
the large out-of-line title view appears, see <Titanium.UI.Window.largeTitleDisplayMode>.
default: false
type: String
type: Boolean
since: "6.3.0"
platforms: [iphone, ipad]
osver: {ios: {min: "11.0"}}

- name: hidesSearchBarWhenScrolling
summary: A Boolean value indicating whether the integrated search bar is hidden when scrolling any underlying content.
description: |
When the value of this property is true, the search bar is visible only when the scroll position
equals the top of your content view. When the user scrolls down, the search bar collapses into
the navigation bar. Scrolling back to the top reveals the search bar again. When the value of
this property is false, the search bar remains regardless of the current scroll position.
You must set <Titanium.UI.ListView.showSearchBarInNavBar> or <Titanium.UI.TableView.showSearchBarInNavBar>
property for this property to have any effect.
default: true
type: Boolean
since: "8.1.0"
platforms: [iphone, ipad]
osver: {ios: {min: "11.0"}}

- name: largeTitleDisplayMode
summary: The mode to use when displaying the title of the navigation bar.
description: |
Expand Down
50 changes: 39 additions & 11 deletions iphone/Classes/TiUIListView.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ @implementation TiUIListView {
CGPoint tableContentOffset;
BOOL isSearched;
UIView *dimmingView;
BOOL isSearchBarInNavigation;
}

#ifdef TI_USE_AUTOLAYOUT
Expand Down Expand Up @@ -193,7 +194,10 @@ - (void)configureHeaders
_searchWrapper = [self initWrapperProxy];
_headerWrapper = [self initWrapperProxy];

[_headerViewProxy add:_searchWrapper];
isSearchBarInNavigation = [TiUtils boolValue:[(TiViewProxy *)self.proxy valueForUndefinedKey:@"showSearchBarInNavBar"] def:NO] && [TiUtils isIOSVersionOrGreater:@"11.0"];
if (!isSearchBarInNavigation) {
[_headerViewProxy add:_searchWrapper];
}
[_headerViewProxy add:_headerWrapper];
}

Expand Down Expand Up @@ -575,17 +579,21 @@ - (void)updateSearchControllerFrames
if (![searchController isActive]) {
return;
}
[dimmingView setFrame:CGRectMake(0, searchController.searchBar.frame.size.height, self.frame.size.width, self.frame.size.height - searchController.searchBar.frame.size.height)];
CGPoint convertedOrigin = [self.superview convertPoint:self.frame.origin toView:searchControllerPresenter.view];
if (isSearchBarInNavigation) {
dimmingView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
} else {
dimmingView.frame = CGRectMake(0, searchController.searchBar.frame.size.height, self.frame.size.width, self.frame.size.height - searchController.searchBar.frame.size.height);
CGPoint convertedOrigin = [self.superview convertPoint:self.frame.origin toView:searchControllerPresenter.view];

UIView *searchSuperView = [searchController.view superview];
searchSuperView.frame = CGRectMake(convertedOrigin.x, convertedOrigin.y, self.frame.size.width, self.frame.size.height);
UIView *searchSuperView = [searchController.view superview];
searchSuperView.frame = CGRectMake(convertedOrigin.x, convertedOrigin.y, self.frame.size.width, self.frame.size.height);

CGFloat width = [_searchWrapper view].frame.size.width;
UIView *view = searchController.searchBar.superview;
view.frame = CGRectMake(0, 0, width, view.frame.size.height);
searchController.searchBar.frame = CGRectMake(0, 0, width, searchController.searchBar.frame.size.height);
[searchViewProxy ensureSearchBarHierarchy];
CGFloat width = [_searchWrapper view].frame.size.width;
UIView *view = searchController.searchBar.superview;
view.frame = CGRectMake(0, 0, width, view.frame.size.height);
searchController.searchBar.frame = CGRectMake(0, 0, width, searchController.searchBar.frame.size.height);
[searchViewProxy ensureSearchBarHierarchy];
}
}

#pragma mark - Public API
Expand Down Expand Up @@ -2188,6 +2196,22 @@ - (void)viewResignFocus

- (void)viewGetFocus
{
if (isSearchBarInNavigation) {
id proxy = [(TiViewProxy *)self.proxy parent];
while ([proxy isKindOfClass:[TiViewProxy class]] && ![proxy isKindOfClass:[TiWindowProxy class]]) {
proxy = [proxy parent];
}
UIViewController *controller;
if ([proxy isKindOfClass:[TiWindowProxy class]]) {
controller = [proxy windowHoldingController];
} else {
controller = [[TiApp app] controller];
}
if (!controller.navigationItem.searchController) {
controller.navigationItem.searchController = searchController;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the iOS 11 guard is required here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already taken care while assigning value to variable 'isSearchBarInNavigation'.
isSearchBarInNavigation = [TiUtils boolValue:[(TiViewProxy *)self.proxy valueForUndefinedKey:@"showSearchBarInNavigation"] def:NO] && [TiUtils isIOSVersionOrGreater:@"11.0"];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vijaysingh-axway Since we still support older Xcode and SDK versions that don't have the searchController property we need the iOS 11 safeguard anyway. Probably something similar to #10729

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janvennemann Wrapped under macro IS_XCODE_9.

}
}

if (isSearched && self.searchedString && ![searchController isActive]) {
isSearched = NO;
[searchController performSelector:@selector(setActive:) withObject:@YES afterDelay:.1];
Expand Down Expand Up @@ -2356,7 +2380,11 @@ - (void)createDimmingView

- (void)showDimmingView
{
dimmingView.frame = CGRectMake(0, searchController.searchBar.frame.size.height, self.frame.size.width, self.frame.size.height - searchController.searchBar.frame.size.height);
if (isSearchBarInNavigation) {
dimmingView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
} else {
dimmingView.frame = CGRectMake(0, searchController.searchBar.frame.size.height, self.frame.size.width, self.frame.size.height - searchController.searchBar.frame.size.height);
}
if (!dimmingView.superview) {
[self addSubview:dimmingView];
[self bringSubviewToFront:dimmingView];
Expand Down
1 change: 1 addition & 0 deletions iphone/Classes/TiUITableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
UIEdgeInsets rowSeparatorInsets;
CGPoint tableContentOffset;
BOOL isSearched;
BOOL isSearchBarInNavigation;
}

@property (nonatomic, assign) BOOL viewWillDetach;
Expand Down
47 changes: 36 additions & 11 deletions iphone/Classes/TiUITableView.m
Original file line number Diff line number Diff line change
Expand Up @@ -1434,17 +1434,21 @@ - (void)updateSearchControllerFrames
if (![searchController isActive]) {
return;
}
[dimmingView setFrame:CGRectMake(0, searchController.searchBar.frame.size.height, self.frame.size.width, self.frame.size.height - searchController.searchBar.frame.size.height)];
CGPoint convertedOrigin = [self.superview convertPoint:self.frame.origin toView:searchControllerPresenter.view];
if (isSearchBarInNavigation) {
dimmingView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
} else {
[dimmingView setFrame:CGRectMake(0, searchController.searchBar.frame.size.height, self.frame.size.width, self.frame.size.height - searchController.searchBar.frame.size.height)];
CGPoint convertedOrigin = [self.superview convertPoint:self.frame.origin toView:searchControllerPresenter.view];

UIView *searchSuperView = [searchController.view superview];
searchSuperView.frame = CGRectMake(convertedOrigin.x, convertedOrigin.y, self.frame.size.width, self.frame.size.height);
UIView *searchSuperView = [searchController.view superview];
searchSuperView.frame = CGRectMake(convertedOrigin.x, convertedOrigin.y, self.frame.size.width, self.frame.size.height);

CGFloat width = [searchField view].frame.size.width;
UIView *view = searchController.searchBar.superview;
view.frame = CGRectMake(0, 0, width, view.frame.size.height);
searchController.searchBar.frame = CGRectMake(0, 0, width, searchController.searchBar.frame.size.height);
[searchField ensureSearchBarHierarchy];
CGFloat width = [searchField view].frame.size.width;
UIView *view = searchController.searchBar.superview;
view.frame = CGRectMake(0, 0, width, view.frame.size.height);
searchController.searchBar.frame = CGRectMake(0, 0, width, searchController.searchBar.frame.size.height);
[searchField ensureSearchBarHierarchy];
}
}

#pragma mark Searchbar-related IBActions
Expand Down Expand Up @@ -1552,7 +1556,9 @@ - (void)updateSearchView
[[searchField searchBar] setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[TiUtils setView:searchView positionRect:wrapperFrame];
[tableHeaderView addSubview:searchView];
[tableview setTableHeaderView:tableHeaderView];
if (!isSearchBarInNavigation) {
[tableview setTableHeaderView:tableHeaderView];
}
[searchView sizeToFit];
}
}
Expand Down Expand Up @@ -1784,12 +1790,16 @@ - (void)setSearch_:(id)search
RELEASE_TO_NIL(searchField);
RELEASE_TO_NIL(searchController);

isSearchBarInNavigation = [TiUtils boolValue:[self.proxy valueForKey:@"showSearchBarInNavBar"] def:NO] && [TiUtils isIOSVersionOrGreater:@"11.0"];

if (search != nil) {
//TODO: now that we're using the search controller, we can move away from
//doing our own custom search screen since the controller gives this to us
//for free
searchField = [search retain];
[searchField windowWillOpen];
if (!isSearchBarInNavigation) {
[searchField windowWillOpen];
}
[searchField setDelegate:self];
[self tableView];
[self updateSearchView];
Expand Down Expand Up @@ -2341,6 +2351,21 @@ - (void)viewResignFocus

- (void)viewGetFocus
{
if (isSearchBarInNavigation) {
id proxy = [(TiViewProxy *)self.proxy parent];
while ([proxy isKindOfClass:[TiViewProxy class]] && ![proxy isKindOfClass:[TiWindowProxy class]]) {
proxy = [proxy parent];
}
UIViewController *controller;
if ([proxy isKindOfClass:[TiWindowProxy class]]) {
controller = [[proxy windowHoldingController] retain];
} else {
controller = [[[TiApp app] controller] retain];
}
if (!controller.navigationItem.searchController) {
controller.navigationItem.searchController = searchController;
}
}
if (!hideOnSearch && isSearched && self.searchedString && ![searchController isActive]) {
isSearched = NO;
searchController.searchBar.text = self.searchedString;
Expand Down
2 changes: 1 addition & 1 deletion iphone/Classes/TiUITableViewProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ - (void)resignFocus
- (NSArray *)keySequence
{
if (tableKeySequence == nil) {
tableKeySequence = [[NSArray arrayWithObjects:@"style", @"search", @"data", @"backgroundColor", nil] retain];
tableKeySequence = [[NSArray arrayWithObjects:@"style", @"showSearchBarInNavBar", @"search", @"data", @"backgroundColor", nil] retain];
}
return tableKeySequence;
}
Expand Down
13 changes: 13 additions & 0 deletions iphone/TitaniumKit/TitaniumKit/Sources/Modules/TiUIWindowProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,17 @@ - (void)setLargeTitleDisplayMode:(id)value
}
}

- (void)setHidesSearchBarWhenScrolling:(id)value
{
ENSURE_UI_THREAD(setHidesSearchBarWhenScrolling, value);
ENSURE_TYPE_OR_NIL(value, NSNumber);

[self replaceValue:value forKey:@"hidesSearchBarWhenScrolling" notification:NO];

if ([TiUtils isIOSVersionOrGreater:@"11.0"] && shouldUpdateNavBar && controller != nil && [controller navigationController] != nil) {
[controller navigationItem].hidesSearchBarWhenScrolling = [TiUtils intValue:value def:YES];
}
}
- (void)setTitlePrompt:(NSString *)title_
{
ENSURE_UI_THREAD(setTitlePrompt, title_);
Expand Down Expand Up @@ -933,6 +944,8 @@ - (void)setupWindowDecorations
SETPROP(@"titlePrompt", setTitlePrompt);
SETPROP(@"largeTitleEnabled", setLargeTitleEnabled);
SETPROP(@"largeTitleDisplayMode", setLargeTitleDisplayMode);
SETPROP(@"hidesSearchBarWhenScrolling", setHidesSearchBarWhenScrolling);

[self updateTitleView];
SETPROP(@"barColor", setBarColor);
SETPROP(@"navTintColor", setNavTintColor);
Expand Down