UISearchbar Hide/Show Issue in UITableView - ios

I have UISearchBar on my UITableView set as its tableViewHeader using InterfaceBuilder. I have hidden it under the UINavigationBar using
self.tableView.contentOffset = CGPointMake(0.0, self.searchBar.frame.size.height);
in my ViewWillAppear method. The problem is that if I go to the detailView and come back to this TableView, if the TableView has more than the screen visible cells, it works fine and hides the SearchBar as it is suppose to do, but if there are less than screen visible cells (i.e. tableview has 0 - 4 cells) then the SearchBar doesn't Hide.
Does anyone know why this is happening?
thanks in advance

Related

Search bar disappears on tap

I am trying to implement UISearchDisplayController searchBar on a view not having a navigationbar on it, my searchBar fixed as
[self.view addSubview:searchBar] and framed above the UITableView having contact numbers. When I tap on searchBar my UISearchBar disappears and the table scrolls up.

Adding a UITableView at run time to a UIViewController that has a NavigationBar - y position issue

I have UIViewController that has a Navigation Bar. At run time, I am adding (on viewDidLoad) a UITable. The problem I have is an inconsistent behaviour of the UITableView y-position.
In viewDidLoad I set the UITableView frame to:
tableView = UITableView(frame: self.view.frame)
then:
self.view.addSubview(tableView)
The problem: on the odd occasion, and only on iphone 4s, the table is added underneath the NavigationBar resulting in the first row not being visible to the user (put another way: y-pos = 0).
If I try to set the y-pos to status bar height + nav bar height the table is always positioned too low (i.e. at the wrong y-pos)
Am I doing something wrong or have I stumbled upon random quirk of ios 9.2 & swift 2?
Hope this will help
Try following in controller's viewDidLoad Method :
self.automaticallyAdjustsScrollViewInsets = NO;
or You can disable it from Attribute Inspector as given in below image.

UICollectionView scrollsToTop

On my controller I have two UICollectionView instances and one UITableView instance. When tableView is on top of view hierarchy and I tap on status bar it does not scroll to top.
I've tried to set scrollsToTop property for collectionViews to NO. But tableView still doesn't scroll to top. Even more: collectionViews still scroll when I tap status bar.
I've gone further. I created a category on UIScrollView, where I swizzled -didMoveToWindow method like this:
- (void)swizzled_didMoveToWindow
{
[self swizzled_didMoveToWindow];
self.scrollsToTop = NO;
}
And it didn't work too! Scroll to top by tapping on status bar still works for all UICollectionView instances!
Please, tell me, what am I doing wrong?

UISearchBar subview strange behaviour

I have added the UISearchDisplayController via Interface Builder, and then I add the UISearchBar to my navigationBar like this
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
It works and looks fine. What I then want to do is add a subview to the searchBar (to act as a placeholder image). I do it by
[self.searchDisplayController.searchBar addSubview:self.imageView];
The problem is, the imageView is not visible. I NSLog its superview and I get a confirmation that it's the searchBar. I can also see that the searchBar has 1 subview, but when I log the subview, it tells me that it's... drum-rolls... the searchBar itself.
How can this be?
EDIT
Interestingly, if I add the searchBar to navigationBar as a subview and the add my imageView, it's visible. The problem is only when the searchBar is being added to navigationBar by self.searchDisplayController.displaysSearchBarInNavigationBar = YES.
Here's how I solved it - I hope this helps someone. The strange thing about adding a searchBar to navigationBar like this
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
is that the 'didLayoutSubviews' is called twice in the viewController, i.e. the searchDisplayController adds the searchBar to the navigationBar with a delay that I was not able to capture through existing delegate methods.
What I ended up doing instead is setting the zIndex of my imageView to maximum
self.imageView.layer.zPosition = MAXFLOAT;
so even if the searchDisplayController adds the searchBar to the navigationBar with a delay, the imageView still stays on top.

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