Table View Hide Searchbar until scrolled - ios

I got a table view set up, and a working search bar - NO XIB OR STORYBOARD involved. Above the search bar I have label showing the numbers of entries in the table view and some other stuff. Well now I want the search bar + label hidden until the user scrolls up (like in Music App). This is the setup of my search bar
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
self.searchBar.showsCancelButton = YES;
[self.view addSubview:self.searchBar];
self.searchBar.delegate = self;
self.tableView.contentInset = UIEdgeInsetsMake(self.searchBar.frame.size.height,0, 0, 0);
[self.tableView setTableHeaderView:self.searchBar];
[self.tableView setContentOffset:CGPointMake(0,88) animated:YES];
[self.zsearchDisplayController setActive:NO animated:YES];
This is my label:
tableCountDisplay = [[UILabel alloc]initWithFrame:CGRectMake(5, -44, 155, 44)];
The label is already hidden until the user scrolls. The problem is - I can't get the search bar to hide. If I do
[self.tableView setContentOffset:CGPointMake(0,88) animated:YES];
Then search bar and label are hidden but also the first element of my table view...
If I do 44 or 0 (doesn't matter which of them)
[self.tableView setContentOffset:CGPointMake(0,44 or 0) animated:YES];
the label is hidden, and everything else is visible. Technically 0,44 should be the right offset, but it does not work for some reason.
I'd be really happy about some help!

One of the solutions is to add your searchBar and your label as subViews to a UIView. Then set this UIView as the TableHeaderView. My working sample looks like this:
Screen before scrolling:
Screen after scrolling:

There is not a way to maintain the header of a tableView fixed
1- could use an UIViewController instead of UITableViewController.
2- add subview (UIView) for header (add the searchBarView in this view).
3- and add another subview for the tableview.

What about
tableView.contentOffset = CGPoint(x: 0, y: (tableView.tableHeaderView?.frame.size.height ?? 0))
on wiewWillAppear: ;)

Related

ios uitableview bounce went wrong

I implemented a horizontal table view and it looks like this
The category bar, Dining Shopping something, is a horizontal table view and here is the implementation code.
LogInfo(#"Show Category Table start");
// add categories to be subview controller
self.categoriesTable = [[UITableView alloc] init];
self.categoriesTable.transform = CGAffineTransformMakeRotation(-M_PI * 0.5);
[self.categoriesTable setFrame:CGRectMake(0, 64, SCREENWIDTH, 44)];
self.categoriesTable.showsVerticalScrollIndicator = NO;
self.categoriesTable.showsHorizontalScrollIndicator = NO;
self.categoriesTable.pagingEnabled = YES;
self.categoriesTable.delegate = self;
self.categoriesTable.dataSource = self;
self.categoriesTable.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.superView addSubview:self.categoriesTable];
LogInfo(#"Show Category Table Finished");
self.categoriesTable.backgroundColor= [UIColor redColor];
It works as expect but if I change view, for example, I click any button to go to other view and go back to this view. The tableview looks like this
This problem also happpens even if I disable the bounce effect of the table view. Does anyone know how to solve this problem? Thanks!
Rather than applying a transform to the table to make it horizontal, use a collectionView with a horizontal layout.
Edit: If you want to continue using your current setup, try disabling automaticallyAdjustsScrollViewInsets on your view controller.
self.automaticallyAdjustsScrollViewInsets = NO
Edit 2: If you're curious, since iOS 7 every view controller looks at its topmost/first scroll view and adjusts its contentInsets to compensate for the navigation bar transparency which is enabled by default. In this case of course, such behaviour isn't desired at all.

iOS Fix search bar on top of the UITableViewController?

I'm adding search bar on table header and floating it in scrollViewDidScroll method, but when i scroll without click on search bar(i.e. i go to the view and do scroll) then search bar doesn't stay on top but it scroll up with table however once i click on search bar and click cancel button on search bar and then if i scroll the table, search bar stays on top.here is my code-
-(void)viewDidLoad {
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
UIView *tableHeaderView = [[UIView alloc] initWithFrame:searchDisplayController.searchBar.frame];
[tableHeaderView addSubview:searchDisplayController.searchBar];
[tableView setTableHeaderView:tableHeaderView];
isSearching = NO;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
UISearchBar *searchBar = searchDisplayController.searchBar;
CGRect searchBarFrame = searchBar.frame;
if (isSearching) {
searchBarFrame.origin.y = 0;
} else {
searchBarFrame.origin.y = MAX(0, scrollView.contentOffset.y + scrollView.contentInset.top);
}
searchDisplayController.searchBar.frame = searchBarFrame;
}
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
isSearching = YES;
}
-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
isSearching = NO;
}
Note that I'm using UITableViewController sub class and don't want to change it to UIViewController.
Any help would be appreciated.
Edit: I also using section header in this UITableViewController, in other UITableViewController there is no section header and this code working fine.Is this a problem with section header and table header together?
The reason why your searchbar is scrolling with the table contents is that you have put it directly IN the table, thus making it a child header section of the table. and that section ALWAYS scrolls…
Here is how this this can be achieved. And it is actually quite simple. (The following example relies on Storyboard, but the mechanism is the same whatever you are using) :
1) Use a UIVIewController and NOT a UITableViewController
2) Add a UITableView as the child of the parent UIView
3) Add a UISearchBarController also as a child view of the UIView, NOT as a child of the UITableView (UITableView and UISearchController are siblings)
you should have the following layout :
EDIT : The important thing to remember is to put the UISearchBarController ABOVE the sibling UITableView. Otherwise you may see the UITableView overlap the UISearchBarController when the latter is focused.
EDIT 2 : BTW, if you are using AutoLayout, remember to set the TOP constraint of the tableView relative to the SearchBar…
Run it and admire the result.
Hope this helps.
There is not a way to maintain the header of a tableView fixed
1- could use an UIViewController instead of UITableViewController.
2- add subview (UIView) for header.
3- and add another subview for the tableview.

UISearchBar Overlays UITableView - TableView Can't be moved

I've got a TableView and a Search Bar set up and both work fine so far. The problem is that the UISearchBar overlays the first item of my TableView.
I cannot use XIB or storyboards in this project.
The definition and instantiation of the TableView is in a galaxy in a class (imported by a class I import...) far, far away -- not that easy to access.
I move (instantiate) the SearchBar on the screen with:
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:self.searchBar];
self.searchBar.delegate = self;
I tried moving it to 0,-44 but then the screen does not stay at the SearchBar, it moves back down.
If I try to move the UITableView with:
self.tableView.frame = CGRectMake(0, 0, 320, 500);
it moves the SearchBar as well.
So let's say if I set the origin of the search bar at 0,0 and the origin of the table view at 0,44 I get a screen with (objects in this order):
Grey Space with the height of a search bar/table view cell
Search bar (still overlaying the first cell of my table view
other cells
How can I solve this problem? I think the easiest way would be to add an empty object to my data array so the search bar overlays nothing of importance, but that would not be the nicest way of solving it.
Could anyone tell me how I can add/insert an object to an array and then copy another array into it, beginning with index 1, so my empty object stays in there?
You can use contentInset property on your tableView. Just set the top inset to height of search bar.
self.tableView.contentInset = UIEdgeInsetsMake(self.searchBar.frame.size.height, 0, 0, 0);
This should solve the issue.

How to also animate subview in navigation bar?

I have a subview in my navigation bar. I try to add it by this way:
UIView *customView =
[[UIView alloc] initWithFrame:
CGRectMake(0, 0, image.size.width + label.frame.size.width, 44)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[customView addSubview:imageView];
[customView addSubview:label];
[self.navigationController.navigationBar addSubview:customView];
However, when I try to push the navigation bar, the customView stays in the place, not animating following the sliding navigation bar. How can I achieve the animated subview? Is it even possible? Thanks!
you should not add subview in that way
you have tospecify your view location in the left , right or title view
self.navigationItem.titleView = YOURVIEW;
or choose another location left or right items in this way the the title view will added to the current view if you want to remove it just set it to nil in the place you want and reload it subviews again,
self.navigationItem.titleView = nil;
As you are using
[self.navigationController.navigationBar addSubview:customView];
that means the navigation bar you have create is in App delegate and is common for all the viewControllers in your project,that is why once you add a view on to it you see it on every view you have. Remove your sub-view in
-(void)viewWillDisappear:(BOOL)animated{
[Your subview remove fromSuperView];
[super viewWillDisappear:YES];
}
and add that subview in
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController.navigationBar addSubview:Your subview];
[super viewWillAppear:YES];
}
this will add the subview in that particular view only and remove it as soon as that view is popped or pushed.The code given is not correct to the syntax please give a check on that.

iOS floating button over table view [duplicate]

This question already has answers here:
How to put buttons over UITableView which won't scroll with table in iOS
(10 answers)
Closed 9 years ago.
Is it possible to add a small floating 'settings' button or image-button over a table view? I have buttons on a navigation bar up top, and i dont want to crowd that or remove any
This is possible by adding it to the main window as a subview. Another option and my preferred method: Have a main wrapper view, the table view can be added to that wrapper view as a subview, then add your button as another subview inside of the wrapper view and voila
Perhaps adding a toolbar with buttons at the bottom would be a better approach?
To use this method, you should inherit UIViewController and add tableview in it
(1)add button to self.tableview in viewDidLoad()
button.frame = CGRectMake(0, 0, 50, 50);
[self.tableview addsubview:button];
[self.tableview bringSubviewToFront:button];
(2)set tableview delegate
self.tableview.delegate = self;
(3) add and update button location to self.tableview in scrollViewDidScroll delegate
button.frame = CGRectMake(0, 0, 50, 50);
[self.view addSubview:button];
[self.view bringSubviewToFront:button];
voila~

Resources