I am using UISearchController and the UISearchBar also has a scope bar. The search bar is added as a subview to a UIView which is above the UItableView. I have done it this way since I want the search bar to be always visible even when the tableview is scrolled.
The problem is the scopebar overlaps the first tableview cell
StoryBoard
Scope bar overlapping the tableview cell
How can I prevent this overlapping?, I can't display the searchbar in navigationbar since the scopebar when placed in navigation bar does not get displayed.
This worked for me:
Having the Search Display Controller and SearchBar in the tableview header. Add the heightForHeaderInSection in your TableViewController.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44; //I used the 44, the height of SearchBar
}
In your case you'll need to also add the scope bar height.
Now it will always keep a base height.
This worked for me :
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (self.searchController.active) {
return 44; // with scope
} else {
return 0; // no scope
}
}
Add the search bar and table view on the UIViewController as shown in the image below. Don't overlap the search bar with the table view. I am using this in my app and its working fine for me.
Related
I've had some issues with my table view and the top cell. I'm using grouped Prototype Cells, and I've come across an issue with the spacing between the top bar and the first cell. As I've seen on other posts, I tried using 'adjust scroll insets' however, this created another problem, with the cell being hidden underneath the navigation bar. When I try changing the translucency of the navigation bar, the spacing returns. I've got some links to the images below.
When you use grouped cell, it's make space automatically in the header section. Try that;
-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == 0)
return 5.0;
return 1.0;
}
It's because that you're using Grouped Style. people use this style to add their header for each group.
You should change the style to plain.
In xib, you should change the Style to Plain
Or do this programmaticlly when init the UITableView
UITableView *myTable = [[UITableView alloc] initWithFrame:CGRectZero
style:UITableViewStylePlain];
I am adding a UISearchBar to my UITableView. Like in many apps, the search bar would initially be hidden behind the navigation bar until the user scrolls upwards, revealing it. I use the code bellow to initially hide the search bar which works very well when the table view has enough cells to need scrolling, but does not hide the bar when there are only a few cells in the table view. How can I go about hiding the search bar when there are less than enough cells to cause the table view to scroll.
Current Code:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (!self.layedOutHeader) {
CGPoint contentOffset = self.alertsTableView.contentOffset;
if (contentOffset.y == -64 || contentOffset.y == 0) {
contentOffset.y += CGRectGetHeight(self.alertsTableView.tableHeaderView.frame);
self.alertsTableView.contentOffset = contentOffset;
}
self.layedOutHeader = YES;
}
}
Here is an example of how when only a few cells are present, the search bar is not hidden:
Try to add tableFooterView with the required height to fill the screen when you don't have enough cells.
EDIT: I think I am narrowing down the issue here. I just found an easier way to recreate what I want. If you create a new project with a storyboard, drag on a tableview controller and drag on a Search Bar as a subview of the tableview. If you run this (with some dummy cells), and you scroll so about half of the search bar is showing, the search bar will snap to the top of the view. If you try this by dragging on a UIView instead of a Search Bar, this behavior no longer happens. How can I get this behavior with a UIView as a subview?
Hopefully this made it more clear, thanks guys.
I have a header in my plain style table view, I am implementing it like this:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 51;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
SegmentedHeaderView *header = [[SegmentedHeaderView alloc]initWithFrame:CGRectMake(0, 0, 320, 51)];
return header;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 51;
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
I want the header to scroll with the tableview and not stick to the top, and it does that correctly. However, when I scroll to the top, I want the header to work like the search bar header does in the messages app in iOS 7. In the messages app, when you are scrolling and you stop halfway through the search bar, either the search bar sticks to the top of the view, or the first cell sticks to the top of the view.
How can I implement this so when the user stops scrolling with part of the header view showing the header jumps up?
Edit: The behavior I want can also be seen with the search bar in the Mail app.
In interface builder I have a header view above my UITableView, however in the simulator it is missing, and the Table View seems to be over it since it takes up most of the screen. Any reason for this? Work around?
Interface Builder
Simulator
In your case:
From your interface builder image I can see that table view is subview of view and you header view is also subview of tableview. It happening because tableview is hiding the header view. if you add the header view as subview of tableview then it would appear. But adding subview will not solve your problem because when you will scroll the tableview the subview will be gone.
For solving your problem as I can see that you have a navigation bar in your table view you can add the header view as subview of UINavigationBar.
[self.navigationController.view addSubview:headerView];
Good Practise:
Set the header view for section. Then you don't need to set any custom view at the top of your tableview. Try this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *aView = [[UIView alloc] init];
//Customize the view according to our requirment
return aView;
}
Also implement this:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44.f; // Make this height as you need. Or else you may not see the full view.
}
Btw you can create a subclass of UITableViewController instead of UIViewController.
Hope this helps.. :)
Deselect Extend Edges / Under Top Bars
I'm using two searchbar on top of eachother in my layout like so :
Both search bar have a UISearchDisplayController that display a suggestions table.
My problem is, the top search bar's display controller draws its UITable over the bottom searchbar like so :
I tried to move the bottom table down in the uisearchdisplaydelegate like so:
- (void)searchDisplayController:(UISearchDisplayController *)controller
didLoadSearchResultsTableView:(UITableView *)tableView
{
float yOffset = 50.0f;
tableView.frame = CGRectMake(tableView.frame.origin.x,tableView.frame.origin.y + yOffset,tableView.frame.size.width, tableView.frame.size.height);
}
But even though the method get called at the right time, it doesnt seem to do anything.
Id basically like the top searchbar suggestion table to be located at the same position as the bottom search bar suggestion table
I'm using storyboards and autolayout latest xcode
In iOS 7, you can just set the y origin to the yOffset. If you print out the frame of the table view in
-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
you'll notice that the origin.y is at 0,0. In iOS 6 however, it's different. If you're planning on supporting both versions you'll need a check in there. Try setting the origin.y in the delegate method mentioned above.