UISearchBar Overlays UITableView - TableView Can't be moved - ios

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.

Related

Use UICollectionView with transparent navigation bar

I am building an iOS application and have done all the layouts using storyboards and auto layout.
I have a view controller with the view hierarchy as follows.
And for the collection view I have added constraints as follows,
For this whole view I have put a background image, as shown in the screen shot.
What I want to achieve is to make the navigation bar transparent and get the collection view to scroll only up to the point where the navigation bar is met.
I have tried setting the contentInset of the collection view as follows,
_collectionView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
But there is no change.
I have also tried embedding the collection view inside another UIView, but still the collection view goes out of the bounds of that view as well.
With what I have right now the collection view scrolls as shown in the following screenshot.
Is there any way to resolve this? It'll be a great help if someone can help out.
You can't scroll up over the navigation bar. So, you need customize the bar like navigation bar.
You can use this:
[[UINavigationBar appearance] setBackgroundColor:[UIColor clearColor]];
[[UINavigationBar appearance] setTranslucent:YES];
It works for me.
You could try to set +44 in the y origin of your collection view:
self.collectionView.frame = CGRectMake(self.collectionView.frame.origin.x, 44, self.collectionView.frame.size.width, self.collectionView.frame.size.height);
and leave the contentInset with y=0:
collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

Add UINavigationBar programmatically to UITableViewController

I've been trying to add a UINavigationBar programmatically to my UITableViewController view. I know I can simply embed the UITableViewController to add a navigation bar, but I don't want to do that due to the way my app is setup.
I have tried the following:
var navBar: UINavigationBar = UINavigationBar(frame: CGRectMake(0, 0, 320.0, 64.0))
navBar.delegate = self
self.view.addSubview(navBar)
self.view.bringSubviewToFront(navBar)
However, the UINavigationBar just sits on the tableview (covering the first row) and scrolls with it.
The only way I have managed to make it stay (statically) at the top is to add it to self.tableView.backgroundView. Whilst this stays in position as the tableView scrolls, the tableView runs over the top of it, instead of underneath!
Can someone please shed some light on the view hierarchy here and tell me where I should be adding it?

Table View Hide Searchbar until scrolled

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: ;)

How do I position UITableView programmatically while using iOS7's UINavigationBar?

I have a quite regular UIViewController that is a part of a UINavigationController-hierarchy, which naturally makes the view have a UINavigationBar at the top. As we all know, iOS7's navigation bars are very different from previous versions.
If I drag a UITableView into my view in Storyboard, then the 'frame' for the table view is covering the entire view (I.E [tableView setFrame:self.view.frame];). Even behind the NavigationBar.
This makes so that if I scroll, the content will be faintly visible through the bar.
Contrary to most people, I actually like this.
In my current view controller, I would like to create the UITableView programmatically and place it as a subview here. However, I am unable to achieve the same effect this way.
I have tried
UITableView *table = [[UITableView alloc] initWithFrame:self.view.frame];
[self.view addSubview: table];
This makes the 'top scrolling point' stay behind the navigation bar. Imagine a single cell in a tableView, and it's faintly visible through the top bar. If I scroll down, it pops right up behind it. That's the default 'top position'.
I have also tried
CGRect frame = CGRectMake(0, 'nav.y+nav.height', self...width, self...height-y);
UITableView *table = [[UITableView alloc] initWithFrame:frame];
[self.view addSubview: table];
to simply place the table view in the rect below the UINavigationBar, but then I won't get the scrolling transparency effect behind the bar.
I also tried the first example along with this:
[tableView setContentOffset:CGPointMake(0, 'below navbar')];
which makes it stay exactly where I want it to stay, but as soon as I touch it (scroll and release), it scrolls back up behind the navigation bar, and stays there.
What's the programmatic solution to achieve this effect? Do I have to set the offset every time I scroll too far up, or is there a simpler solution? Along with this iOS7-style, I'd imagine they would add something like [tableView setVisibleFrame:] or something..?
Add the table in storyboard like normal but make sure you have it connected to an outlet (we will call it _tableView for now).
Then in your view controller's -viewDidLoad set the top inset to be the status bar plus the navigation bar:
`self.tableView.contentInset = UIEdgeInsetsMake( (self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height), 0, 0, 0);'
This will make sure that all of the cells will be visible but will also fill the area behind the navigation bar with white so the bar doesn't end up gray.

IOS searchBar - Getting rid of the white space below

I have a view which has two containers: Top_Container and Bottom_Container.
Each Container points to a VC with a TableView.
The Bottom_Container points to a TableView with a searchBar on top.
Whenever the searchBar gets activated in the TableView a white space appears below the searchBar between the searchBar and the greyed zone corresponding to the serachBarTableView (which superposes the TableView).
I have been trying with no success to get rid of this white space with no success.
Anybody has an idea how to customize:
- the white space which appears below the searchBar ?
- the greyed zone (searchBar TableView ?) on top of the TableView which appears whenever the searchBar gets active ?
Thank you.
Try with following code:
CGRect rect = self.searchBar.frame;
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, rect.size.height-2,rect.size.width, 2)];
lineView.backgroundColor = [UIColor clearColor];
[self.searchBar addSubview:lineView];
It is fix.
I had similar problem and in my case it was caused by opaque NavigationBar. When I set NavigationBar to translucent, then the underlaying UITableView is correctly aligned to the active UISearchBar. (note: I'm using UISearchDisplayController in my view controller)
In your case maybe you can move (and animate) the underlaying table view in UISearchDisplayDelegate's methods willBeginSearch and willEndSearch. If you are using UISearchBar only, then you need to subclass it and override becomeFirstResponder and resignFirstResponder methods and implement the "table view moving" code there.
I just fixed this problem in my own code. For me, the issue was caused by 2 lines of code.
self.viewController.edgesForExtendedLayout = UIRectEdgeNone;
self.navigationBar.translucent = NO;
After removing both these lines, everything worked as expected.

Resources