How to add search bar in tabbar controller in ios? - ios

I am working on a project in which i have to use search bar on tab bar controller on the top. i tried so many codes but not working i am sharing my code. please help me to overcome this problem.
UISearchBar *search = [[UISearchBar alloc] init];
search.frame = CGRectMake(5 ,5, 300,45);
search.delegate = self;
search.showsBookmarkButton = NO;
search.placeholder = #"Search/Select a Creative Service";
search.barTintColor = [UIColor blackColor];

Set your search bar as the title view:
self.navigationItem.titleView = search;

Create one baseviewcontroller and add your UISearchBar in it. After that you can inherit your each tab's ViewController from that baseviewcontroller. So, in each tab of your UITabBarController you will get that UISearchBar.
The code you have written to create a UISearchBar is correct. Just add it to the view of the baseviewcontroller. Also, please ensure that frame is set correctly.

I've been struggling with that too until I tried something and it turned out it's very simple !
All you need to do is to add UISearchBar and a Container View in one UIViewController and then from that Container View set the segue to your UITabBarController and here you go ! One search bar across all the tabs and the solution will also help in separating the logic of the search functionality from the tabs functionality as you will not be forced to implement a search view across all your tabs.
Here I created a view controller with 1 UIView containing a search bar and an account icon and also containing a Container View (The blue part) which will hold out TabBarController

Related

iOS How to implement Tab Bar View Controller with no tab bar item selected?

I have gone through almost all question related to this on Stack Overflow
What I what to do is to show TabBarViewController as in the image.
Is there any way to achieve this in my tab bar view controller app?
NO, there needs to be at least one Tab selected. By default the Tab bar will select the first view controller object.
if you want to use Tabbar more flexible, you can use a customView instead of TabBarViewController's tabor, just like:
UIView *customTabBar = [[UIView alloc] init];
customTabBar.frame = self.tabBar.bounds;
[self.tabBar addSubview:customTabBar];

How to make Navigation Bar and Tab bar always visible on every screen even after implementing PushView controller

I have a iPhone app in which need to add UISearchbar on navigation bar, so that user can search from any where inside the app. When User Searches it displays search result in a Tableview controller and User can select any row which pushes to other controller.
Below is the Structure which i have in StoryBoard
Navigation Controller
Tab Bar Controller
View Controller1
View Controller2
View Controller3
View Controller4
I can successfully add the UIsearchbar on Navigationbar in ViewController 1
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:SEARCH]];
imageView.frame = CGRectMake(0, 0, 20, 20);
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(searchTapped:)];
[imageView addGestureRecognizer:tapGesture];
self.searchButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.searchButton.style = UIBarButtonItemStylePlain;
self.searchButton.target = self;
[self.searchButton setTintColor:[UIColor whiteColor]];
self.tabBarController.navigationItem.rightBarButtonItem = self.searchButton;
I can successfully implement the Search results as well in Tableview controller but when user select row it Pushes to different controller. Below is the code which implmenetd to push to different controller.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
EntityViewController *entityController = [mainStoryboard instantiateViewControllerWithIdentifier:ENTITYIDENTIFIER];
entityController.dataDic = result;
[self.navigationController pushViewController:entityController animated:YES];
The problem here is when it pushes to different controller the TabBar is completely hidden and Navigation bar right button is completely Hidden.
I tried to Unhide the tab bar and Navigation bar but no effect. I tried all possibilities but nothing worked.
Can some one please let me know what is right approach to solve this issue.
Thanks In Advance.
Have you tried switching the order of the UINavigationController and the UITabController?
Right now, it seems like you have a UINavigationController with a UITabController "inside" it. So, when you push something else to the UINavigationController, the UITabController is being pushed aside, as it is no longer relevant.
If you were to have the UITabController on the top most level, with the UINavigationController at each of the tabs (or only on the relevant ones), when you'll push another view controller to that UINavigationController, it won't affect the tab bar, since it will still be on the top most level, completely unaffected by the push.
I hope this helps. Good Luck!
Thanks for the help. I resolved the issue by making Tab Bar controller as TOP View and Navigation controller for each view ( as per requirement. I made SearchController separately so that user can search from any where inside the app.
I updated this as it can be help full for those who needs to implement similar kind of requirement.
If there is a better approach of implementation please do provide your comments.
Thanks

UIView overlapping Navigation Bar

In a viewController I programmatically create a UIView that has the same height of the screen. The problem is that navigation bar is still visible and clickable, but I want it to go under the new view. How can I do that?
EDIT: this is a screenshot of what I have now
Not sure if this is what you actually want, since hiding it is a quite acceptable thing to do. However you can hide the rightButtonItem and disable the left one:
self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.backBarButtonItem.enabled = NO;
And to get back your right bar button, if you need it again somewhere:
self.navigationItem.rightBarButtonItem = self.*whatever*ButtonItem;
See if that works. I'm away from my Mac at the moment, so can't check it myself.
Right now you have taken navigation controller as a root view
controller (Maybe),In this case navigation controller overlaps the
UIVewController's view that's why it comes on the view so you need to
hide the Navigation controller.
What about making it hidden?
self.navigationController.navigationBarHidden = YES;

how do I display different views in a view controller using a tab bar

I have a view controller that has a map view and a second view under the a tab bar. How do I go about updating the second view when I press buttons on the tab bar?
I tried:
LocationNotesViewController lnvc = new LocationNotesViewController();
lnvc.View.Frame = MainPageTabBarView.Frame;
MainPageTabBarView = lnvc.View;
Nothing happens...the view doesn't update.
I want to update the second view with different things when a user clicks on the tabbar...
If you put a UIView underlying whatever data you want displayed in it, you can use the IBAction of the tabBar to programtically cahnge out the contents of the UIView.
Or you could have the IBActions of the tabbar create new UIViews on the fly, containing whatever you want inside.
Code to do this would be like explained here: http://www.programmerscountry.com/creating-uiviewcontrols-programatically/.
Not exactly the same, but you will understand how it works from that answer.
To switch UIViewControllers use this piece of code:
UIViewController *viewController =
[self.storyboard instantiateViewControllerWithIdentifier:#"4"];
[self presentViewController:a animated:YES completion:nil];
I think this is a work around - but I was able to make this happen by doing:
LocationNotesViewController lnvc = new LocationNotesViewController();
lnvc.View.Frame = new RectangleF(MainPageTabBarView.Frame.X, MainPageTabBarView.Frame.Y - MainPageTabBarView.Frame.Y, MainPageTabBarView.Frame.Width, MainPageTabBarView.Frame.Height);
MainPageTabBarView.AddSubview(lnvc.View);

Setting a title and a UIBarButtonItem in WePopoverController

I am implementing a WEPopoverController and would like to set the title and put a button on top of the popover. Is it possible to do that with this controller?
This is what I have so far. I am loading TableViewController into the popup.
I tried to set self.title=#"title"; in the tableviewcontroller's viewdidload but that didn't help. I only see the tableview with borders inside.
I tried to create a tableview controller in storyboard and programmatically load into the popup but i couldn't resize it. Plus i am not sure if that would be good programming practice.
This is how i load the popuptableview. Would it be easier to use a UIViewController? I don't really need the tableview.
PopUpTableViewController *popUpTable = [[PopUpTableViewController alloc]initWithStyle:UITableViewStylePlain];
// TestViewController *testView = [[TestViewController alloc]init];
self.popoverSettingsController = [[WEPopoverController alloc]initWithContentViewController:popUpTable];
[self.popoverSettingsController presentPopoverFromRect:frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown|UIPopoverArrowDirectionUp
animated:YES];
I looked through the preference options but didn't see anything about title. Would i have to resize the container?
You should create a navigation controller with your popUpTable as root view controller and then use the navigation controller as the content view controller for the pop over.
If the above is done, then your logic of self.title inside popUpTable and creating bar button item will work!

Resources