UISearchController inside UIViewController with view before it - ios

My task is to implement inside UIViewController (Not TableVC) search bar with UISegmentedControl. I want to make something like
But my result is
How to prevent UINavigationBar from hiding (iOS 8)? And how to place UISearchBar below segmentedControl?
Inside viewDidLoad
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = self;
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.tableView.tableHeaderView = self.searchController.searchBar;
#pragma mark - UISearchControllerDelegate & UISearchResultsDelegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = self.searchController.searchBar.text;
[self updateFilteredContentForAirlineName:searchString];
if (self.searchController.searchResultsController) {
UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController;
MBQuestionsSearchResultVC *vc = (MBQuestionsSearchResultVC *)navController.topViewController;
vc.searchResults = self.searchResults;
[vc.tableView reloadData];
}
}
I also try to make constraints programmatically
- (void)updateViewConstraints
{
[self.searchController.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.segmentedControl.mas_bottom).with.offset(10.f);
make.leading.mas_equalTo(self.view);
make.trailing.mas_equalTo(self.view);
make.height.mas_equalTo(44.f);
}];
[super updateViewConstraints];
}
But the result is not good
Thanks in advance :(

Related

Can't push a view onto searchResultsController

I have a TableViewController, a SearchResultsController and a WebViewController. When I click on a cell in the TableViewController it pushes the WebViewController opening a url that is related to the cell.
The TableViewController has a UISearchController which filters the results into the SearchResultsController however when a cell is clicked in the SearchResultsController the WebViewController doesn't get pushed.
Here is the code for the didSelectRowAtIndexPath for the SearchResultsController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
WebViewController *webViewController = [[WebViewController alloc] init];
NSString *extensionURL = (self.searchResults[indexPath.row])[#"url"];
NSURL *baseURL = [NSURL URLWithString:#"http://www.baseurl.com"];
NSURL *URL = [NSURL URLWithString:extensionURL relativeToURL:baseURL];
webViewController.title = (self.searchResults[indexPath.row])[#"title"];
NSLog(#"%#", URL);
webViewController.URL = URL;
[self.navigationController pushViewController:webViewController
animated:YES];
}
I included the NSLog to see if anything happened when a cell was clicked and it does display the url in the console. This makes me think the problem is with the navigationController.
Looking online it seems that the UISearchController should be wrapped in a UISearchContainerViewController which should then be put in the navigationController. I'm still new to app development and can't figure out how or where to do this.
I call my SearchResultsController in the viewDidLoad of my TableViewController like so:
- (void)viewDidLoad
{
[super viewDidLoad];
SearchResultsViewController *controller = [[SearchResultsViewController alloc] initWithStyle:UITableViewStylePlain];
[self addObserver:controller forKeyPath:#"results" options:NSKeyValueObservingOptionNew context:nil];
self.searchController = [[UISearchController alloc] initWithSearchResultsController: controller];
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.delegate = self;
self.definesPresentationContext = YES;
UISearchBar *searchBar = [[UISearchBar alloc] init];
searchBar = self.searchController.searchBar;
searchBar.searchBarStyle = UISearchBarStyleMinimal;
UITextField *searchField = [searchBar valueForKey:#"_searchField"];
searchBar.barTintColor = [UIColor whiteColor];
searchBar.tintColor = [UIColor whiteColor];
self.navigationItem.titleView = searchBar;
}
Is this where I should be embedding my SearchResultsController?
When doing search you will present the NewModalViewController so its simple all you need to do is some steps for pushing viewcontroller when you present search controller
1)self.definesPresentationContext = YES;
After this you should push your viewcontroller like
2) [self.presentingViewController.navigationController pushViewController:sec animated:YES];
Note:Present modalviewcontroller doesnt have Navigation controller so normal push coding wont work as it is not allocated to navigation.
Enjoy.....
You can create a property in your SearchResultsController:
#property (nonatomic, strong) UINavigationController *navController;
Then in your updateSearchResultsForSearchController in your TableViewController set the navController = self.navigationController.
Then you can do this in your SearchResultsController's didSelectRowForIndexPath:
[self.navController pushViewController:webViewController
animated:YES];
This approach worked for me after failing to get Arun's above mentioned solution to work.

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.

UISearchBar within UIViewController

I am trying to add a search bar programmatically to an UIView (not a UITableView). For the moment, I am just trying to set the delegates properly so that when I click the "enter" button, a message is printed:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(#"enter button clicked");
[searchBar resignFirstResponder];
}
I already have some code working for an UITableViewController that I am trying to adapt. It looks like this (I am skipping all the parts of the code that are not relevant to the present discussion):
#property (nonatomic, strong) UISearchController *searchController;
#property (nonatomic, strong) UITableViewController *resultsTableController;
- (void)viewDidLoad {
[super viewDidLoad];
self.resultsTableController = [[SearchResultsController alloc] init];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
self.tableView.tableHeaderView = self.searchController.searchBar;
}
Now the only line that doesn't transpose to the UIViewController case is the last one
self.tableView.tableHeaderView = self.searchController.searchBar;
My UIViewController is defined with a xib which contains a UIView called myView and a UISearchBar called searchBar. So I tried doing
self.myView = self.searchController.searchBar;
or
self.searchBar = self.searchController.searchBar;
but in the first case the searchBardoesn't even show and in the second case the delegation to self seems lost (nothing happens when I click enter).
What am I doing wrong ?
This is how you would add a UISearchBar to a UIViewController programmatically:
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreenWidth, 40.0)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.searchBar.tintColor = kRunnerSearchBarTintColor;
if ([RunnerUtilities isIOS7orAbove]) {
self.searchBar.searchBarStyle = UISearchBarStyleProminent;
[self.searchBar setBarTintColor:[UIColor colorWithRed:185.0/255.0 green:195.0/255.0 blue:202.2/255.0 alpha:0]];
}
self.searchBar.delegate = self;
[self.view addSubview:self.searchBar];
Well, this is no more different than adding it from XIB file. First, search bar is a added as a subview to view of view controller and should be connected to search bar outlet defined in ViewController. Second, UIViewController must be set as delegate of the search bar. Please ensure you are following this and this should work.
EDIT::
If you are trying to use UISearchController within UIViewController, please try with following code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any custom init from here...
// Create a UITableViewController to present search results since the actual view controller is not a subclass of UITableViewController in this case
UITableViewController *searchResultsController = [[UITableViewController alloc] init];
// Init UISearchController with the search results controller
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
// Link the search controller
self.searchController.searchResultsUpdater = self;
// This is obviously needed because the search bar will be contained in the navigation bar
self.searchController.hidesNavigationBarDuringPresentation = NO;
// Required (?) to set place a search bar in a navigation bar
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
// This is where you set the search bar in the navigation bar, instead of using table view's header ...
self.navigationItem.titleView = self.searchController.searchBar;
// To ensure search results controller is presented in the current view controller
self.definesPresentationContext = YES;
// Setting delegates and other stuff
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
}

UINavigationBar with UISegmentedControl partially covers childViews

I have read many other threads on this and the Apple docs, but haven't found a solution yet for my particular problem.
My app uses a UITabBarController as the rootViewController, and in one of the tabs I have a UISegmentedControl in the navigationBar to switch between three child UITableViewControllers.
(In the real app two of the childVCs are a custom UIViewController, I'm just using three UITableViewControllers for the sample app).
The segmentedControl setup and the switching all works fine. The thing that goes wrong is that only the first UITableViewController is shown correctly. For the second and third one, part of the first cell is hidden under the navigationBar. When I click through all three, the first one is still ok.
I have made a little sample app to show what's going on, using very bright colors for demonstration purposes: https://www.dropbox.com/s/7pfutvn5jba6rva/SegmentedControlVC.zip?dl=0
Here is also some code (I'm not using storyboards):
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
FirstViewController *fvc = [[FirstViewController alloc] init];
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController: fvc];
SecondViewController *svc = [[SecondViewController alloc] init];
UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController: svc];
// Initialize tab bar controller, add tabs controllers
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = #[firstNavigationController, secondNavigationController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
// FirstViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = #"One";
self.view.backgroundColor = [UIColor orangeColor];
UITableViewController *vc1 = [[UITableViewController alloc] init];
UITableViewController *vc2 = [[UITableViewController alloc] init];
UITableViewController *vc3 = [[UITableViewController alloc] init];
vc1.view.backgroundColor = [UIColor redColor];
vc2.view.backgroundColor = [UIColor blueColor];
vc3.view.backgroundColor = [UIColor greenColor];
self.viewControllers = #[vc1, vc2, vc3];
self.segmentTitles = #[#"Red", #"Blue", #"Green"];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems: self.segmentTitles];
[self.segmentedControl addTarget: self
action: #selector(segmentClicked:)
forControlEvents: UIControlEventValueChanged];
self.navigationItem.titleView = self.segmentedControl;
self.segmentedControl.selectedSegmentIndex = 0;
// set the first child vc:
UIViewController *vc = self.viewControllers[0];
[self addChildViewController: vc];
vc.view.frame = self.view.bounds;
[self.view addSubview: vc.view];
self.currentVC = vc;
}
- (void)segmentClicked:(id)sender
{
if (sender == self.segmentedControl)
{
NSUInteger index = self.segmentedControl.selectedSegmentIndex;
[self loadViewController: self.viewControllers[index]];
}
}
- (void)loadViewController:(UIViewController *)vc
{
[self addChildViewController: vc];
[self transitionFromViewController: self.currentVC
toViewController: vc
duration: 1.0
options: UIViewAnimationOptionTransitionFlipFromBottom
animations: ^{
[self.currentVC.view removeFromSuperview];
vc.view.frame = self.view.bounds;
[self.view addSubview: vc.view];
} completion: ^(BOOL finished) {
[vc didMoveToParentViewController: self];
[self.currentVC removeFromParentViewController];
self.currentVC = vc;
}
];
}
So obviously my question is, why does this happen, and what can I do to fix it?
Edit: adding screenshots.
EDIT: Based on the answer below I changed the code in the animation block to:
[self.currentVC.view removeFromSuperview];
if ([vc.view isKindOfClass: [UIScrollView class]])
{
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, self.bottomLayoutGuide.length, 0);
[UIView performWithoutAnimation: ^{
vc.view.frame = self.view.bounds;
((UIScrollView *)vc.view).contentInset = edgeInsets;
((UIScrollView *)vc.view).scrollIndicatorInsets = edgeInsets;
}];
}
else
{
vc.view.frame = self.view.bounds;
}
[self.view addSubview: vc.view];
Now it works. I'm going to try this with a custom UIViewController as well.
The issue is that you do not set the correct content inset to each table view. The system attempts to do it for you, but I guess your setup is too complex for it, and it only does it for the first tableview that is loaded in viewDidLoad. In your loadViewController: method, when replacing the currently displayed view, make sure to set both the contentInset and scrollIndicatorInsets to the values of the previous view. I think the system will manage to set the correct insets later, in case you rotate to landscape. Try it. If it doesn't, you will need to do it on your own in viewDidLayoutSubviews.

Resources