UISearchController into UIView iOS Objective C - ios

I'm building an iOS app and I'm creating a custom UIView which contains UIWebView some UIButton and I'm trying to add UISearchController the problem is that the view doesn't have UINavigationController so I created one and I tried to put my UISearchBar into titleView but it doesn't work .
Here is my code, any idea please !!
- (void)initView {
self.navigationController = [[UINavigationController alloc] init];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
[self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:#"bakground1"]init] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.tintColor = [UIColor hx_colorWithHexRGBAString:#"#332532"];
[self initializeSearchController];
self.webView = [[MapWebView alloc] initWithFrame:CGRectMake(0, 100, widthtScreen, heightScreen)];
self.webView.mapWebViewDelegate = self;
[self addSubview:self.webView];
}
- (void)initializeSearchController {
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.navigationController.definesPresentationContext = YES;
self.searchController.hidesNavigationBarDuringPresentation = false;
self.searchController.accessibilityElementsHidden= true;
self.searchController.dimsBackgroundDuringPresentation = true;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.navigationController.navigationItem.titleView = self.searchController.searchBar;
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
}

Related

UISearchController issues iOS8 vs iOS9

I ran into a problem where the UISearchController, when it animates the searchBar into the navigationBar, leaves the statusBar transparent and not the same color as the searchBar, like below:
I searched SO, and the below solution inspired by this question
- (void)willPresentSearchController:(UISearchController *)searchController {
self.navigationController.navigationBar.translucent = YES;
}
- (void)willDismissSearchController:(UISearchController *)searchController {
self.navigationController.navigationBar.translucent = NO;
}
fixes the problem for iOS9, but not for iOS8. I tried setting
self.extendedLayoutIncludesOpaqueBars = YES;
but with no effect. Also, setting
self.edgesForExtendedLayout = UIRectEdgeAll;
hides the initial searchBar under the navigationBar, so I cannot use it.
Here's my code for initiating a UISearchController under an opaque UINavigationBar:
UITableViewController *searchResultsController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.searchController.searchBar.barTintColor = [UIColor whiteColor];
self.searchController.searchBar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 44.0); // Required for iOS8, not iOS9 though
[self.view addSubview:self.searchController.searchBar];
Any suggestions how to fix this for iOS8?

UISearchController in UITabBarController status bar issues

I have been having this issue with the status bar and tab bar.
Here is my code:
self.searchResultsController = [BZDashboardSearchResultsController new];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController];
self.searchController.hidesBottomBarWhenPushed = YES;
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = YES;
self.definesPresentationContext = YES;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.view.backgroundColor = [UIColor whiteColor];
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
[self presentViewController:self.searchController
animated:YES
completion:nil];
I want to hide the tab bar place the search bar under the status bar. My view controller hierarchy looks like this:
UITabBarController
--UINavigationController
---View Controller where the above code is executed.
In my app delegate, I set my UITabBarController subclass as the root view controller:
self.window.rootViewController = tabBarViewController;
The init method of the subclass is this:
-(instancetype)init {
self = [super init];
if (self) {
UINavigationController *workspaceNavigationController = [[UINavigationController alloc] initWithRootViewController:self.workspaceController];
self.userProfileViewController.showLogoutButton = YES;
UINavigationController *userProfileNavigationController = [[UINavigationController alloc] initWithRootViewController:self.userProfileViewController];
self.viewControllers = #[workspaceNavigationController, userProfileNavigationController];
}
return self;
}
self.workspaceController has a search button in its navigation bar the code that presets UiSearchController.

UISearchController UISearchBar background

When adding a UISearchController to a UITableView header view, it is impossible to set the background behind the UISearchBar.
The second picture is showing the view when dragging to the bottom.
The UITableView is set in the Storyboard without any headers.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
// Setup appearance
self.searchController.searchBar.barStyle = UIBarStyleBlack;
self.searchController.searchBar.barTintColor = BLUE;
self.searchController.searchBar.tintColor = RED;
// Setup logic
self.searchController.searchBar.scopeButtonTitles = #[ NSLocalizedString(#"students", nil), NSLocalizedString(#"professors", nil), NSLocalizedString(#"rooms", nil) ];
self.searchController.searchBar.delegate = self;
self.searchController.searchResultsUpdater = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = YES;
I tried adding the background to the storyboard, to the superview, the UITableView, and manually to the UISearchBar.
More code available here.
You can try this:
UIView* topview = [[UIView alloc] initWithFrame:CGRectMake(0, -480, self.view.frame.size.width, 480)];
topview.backgroundColor = BLUE;
[self.tableView addSubview:topview];
I have tested with a UITableViewController and it works fine.
Inspired from this answer

Avoid UISearchBar resizing

I have a UITableView as a UIView subview. This table view has a UISearchBar, which is working correctly, but it's changing it's position when it becomes first responder:
Code:
self.searchBar = [UISearchBar new];
self.searchBar.delegate = self;
self.searchBar.placeholder = #"Buscar";
self.searchBar.text = #"";
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.tableView.tableHeaderView = self.searchBar;
Is there anyway to avoid this change? I'd like it to remain at the same place when selected.
Thanks!
Here's the solution: http://petersteinberger.com/blog/2013/fixing-uisearchdisplaycontroller-on-ios-7/
UISearchDisplayController is pretty bad on iOS 7.

Custom Navigation Transitions on iOS 7, how to manage the keyboard?

TabBarController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
PlaylistViewController *playlistViewController = [[PlaylistViewController alloc] init];
UIViewController *otherViewController = [[UIViewController alloc] init];
UINavigationController *playlistNavigationController = [[UINavigationController alloc] initWithRootViewController:playlistViewController];
playlistNavigationController.delegate = self;
self.viewControllers = #[playlistNavigationController, otherViewController];
playlistNavigationController.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:kTabBarTagPlaylist];
playlistNavigationController.tabBarItem.title = #"Playlist";
otherViewController.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:kTabBarTagOther];
playlistNavigationController.navigationBar.tintColor = [UIColor redColor];
}
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
if (operation == UINavigationControllerOperationPush) return [[FadeTransition alloc] init];
else if (operation == UINavigationControllerOperationPop) return [[SlideTransition alloc] init];
return nil;
}
PlaylistViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
UIBarButtonItem *storeBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Store"
style:UIBarButtonItemStylePlain
target:self
action:#selector(test)];
self.navigationItem.leftBarButtonItem = storeBarButtonItem;
UIBarButtonItem *currentlyPlayingButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Now playing"
style:UIBarButtonItemStylePlain
target:self
action:#selector(test)];
self.navigationItem.rightBarButtonItem = currentlyPlayingButtonItem;
self.navigationItem.title = #"Playlist";
self.playlistTableViewController = [[PlaylistTableViewController alloc] initWithStyle:UITableViewStylePlain];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
searchBar.placeholder = #"Search";
searchBar.delegate = self;
self.playlistTableViewController.tableView.tableHeaderView = searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
[self addChildViewController:self.playlistTableViewController];
[self.view addSubview:self.playlistTableViewController.view];
self.playlistTableViewController.view.frame = self.view.bounds;
self.playlistTableViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.playlistTableViewController didMoveToParentViewController:self];
}
FadeTransition.m
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 0.5;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
/* view controllers */
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
/* views */
UIView *containerView = [transitionContext containerView];
[containerView addSubview:fromVC.view];
[containerView addSubview:toVC.view];
/* settings */
toVC.view.alpha = 0.0f; /* not visible at start */
[UIView animateWithDuration:[self transitionDuration:transitionContext]
delay:0
options:UIViewAnimationOptionTransitionNone
animations:^{
toVC.view.alpha = 1.0f; /* 100% visible at the end */
}
completion:^(BOOL finished) {
[fromVC.view removeFromSuperview];
[transitionContext completeTransition:YES];
}];
}
This is how it looks like:
When I touch one of the results, a new UIView gets pushed and animated by FadeTransition.
The keyboard automatically gets slided down, but I need to leave the keyboard opened (it should stay behind), how can I do it?

Resources