iOS13 - UITableView + UISearchController with custom hidesSearchBarWhenScrolling behavior - ios

On iOS 13, if you set hidesSearchBarWhenScrolling to true, the view controller will hide the search bar, and you have to scroll down to reveal it.
If you set it to false then it is immediately shown and it won't disappear.
I think this is a reasonable behavior, assuming that the end users are all versed in the way the Apple products work. Given that's not seem to be the case, I would like to show the searchbar at first visit, but then if the user starts scrolling up, the search bar is hidden. So this is a mixed behavior of the true/false.
This worked on iOS12, where I set the hidesSearchBarWhenScrolling = true in the viewDidAppear, but now on iOS 13 it's not the case. The table scrolls, but the searchbar stays on top instead of scrolling it together with the tableview(btw this is only because the screen is not rendered again).
Any idea how to go about it? I tried changing the content offset of the tableview, but no luck really.

For me it worked after adding following lines in viewDidLoad() method:
navigationController!.navigationBar.sizeToFit()
No need to specify hidesSearchBarWhenScrolling as I think by default it is true, but if you still see the search bar then you can set it true to hide it.

I faced the same issue as you and looks like such issue appears in iOS 13 only. In iOS 14 your solution works perfectly.
To make it work in iOS 13 you need to add
self.navigationItem.hidesSearchBarWhenScrolling = true
self.navigationController?.view.setNeedsLayout() // add this line
in viewDidAppear
It works for me in simulator iOS 13.7 I hope it work for you as well.

Related

TableView pushed down after view change

I'm kind of new with ios development with swift 4, so I'm testing some functionalities.
I have a tableview inside a normal UIViewController:
The space left blank is intentionally, as I have some custom tabs.
When I show a detail from a table cell element, and then press back button, the table view gets pushed down (and up). Please see this where I show the problem.
The iphone is a SE with ios10. In simulations with iOS11, this does not happen. Developing in MacOS Sierra 10.12.6 with xCode 9.2.
Any clue on how to fix this?
EDIT:
Using the view debugger, I got this:
The selected area is a UITableWrapperView element. Behind it, is the UITableView, which preserve its constrains.
set
self.tableView.bounces = false
Or put this in viewDidAppear
let offset = CGPoint.init(x: 0, y:0)
self.tableView.setContentOffset(offset, animated: false)
It can happen because of SafeArea... Check you constraint which connects tableView top with navigation bar.
You can also try placing blank UIView between Navigation bar and tableView. Your custom tabs should be in this view. So tableView will be connected with the nearest view (which you have just added) and should not bounce.
Thats a really weird behavior. I recommend you to use the Xcode view debugger. You would be able to examine dimensions and constraints values by using size inspector.
The code where you set up your tableview, try implementing it in the method viewDidLayoutSubviews(in case you are doing it in viewDidLoad).
override func viewDidLayoutSubviews() {
//mention your tableview setup code here
}
A similar thing happened to me, and this method came quite handy. Hope this helps.
Try this for iOS 11.0
tableView.contentInsetAdjustmentBehavior = .never

How to move a table content to the top of the screen?

I am still new to iOS and Swift and I am using Swift4 and Xcode9 to edit an iOS project which is made with a prior versions of Xcode and Swift (Xcode8 and Swift3). In the project there is a TableViewController used. So the problem is when I open the project in Xcode9 and run it, it shows some space on the top. I tried changing y values, but didn't work. My questions are,
How to solve the upper mentioned problem. (There is a question like this in Stack Overflow already. But it's not due to different Xcode version. But I even tried all the suggestions in the answers. But none of them worked for me.)
When there is a text label or something on the top of the table content, I can remove that space, from the top, but it goes to in between that label and the table added. And the label is unable to get a click action or something when it's moved to the top. How to solve that?
Any suggestion/ answer will be highly appreciated. Thank you!
You can try this
if #available(iOS 11.0, *) {
yourTableView.contentInsetAdjustmentBehavior = .never
}
Ok I'm going to attempt to answer your question but I wasn't totally positive since you didn't include any code or screenshots.
Firstly. If you mean that there seems to be a gap between where the tableView starts and where the first cell is displayed, this is correct. You can fix that by doing:
tableView.contentInset = .zero
this means that any content inside the tableView starts goes all the way to the edges.
Now for the label receiving touches. You want to look at user interaction enabled values on the storyboard:
Whenever you have views that stack on top of each other, the view UNDER will not receive touches if the view on top has user interaction enabled. If you want a touch to bleed through another view you either can
1) avoid the issue by not stacking views on top of each other (often unavoidable, like putting a label on a view and wanting the background view to do something)
2) turn off the user interaction on the view on top so the view on bottom gets the touch
tableView.setContentOffset(CGPoint.zero, animated: true)
this code will move

Tweak UI Navigation on iOS 11

I have a problem with a navigation bar in iOS 11.
I use this code:
[UINavigationBar appearance].prefersLargeTitles = YES;
...to set a new style for my app. It works well untill a user pull to refresh on the table view; then it breaks.
This is before pull-to-refresh:
...and this is _after:
Note: I use the table view controller's built-in pull-to-refresh control.
I searched for a solution but it still eludes me. If someone knows how to fix this please drop some advice.
Thanks for the support :)
I have catch this bug too, and we have found the solution.
You must constraint your UITableView to superview (contentView of your view controller), after that large title and all related views starts to work correct.
Like this:
While I do not claim this is the solution for every situation the error occurs in, setting the navigationBar's isTranslucent property to true (which is also the default value) fixed the problem for me.
If you want to keep your navigation bar non-translucent you can use the following code:
navigationBar.barStyle = .blackOpaque

UISearchBar for UISearchController iOS 8 regain focus after resignFirstResponder

I'm using the new UISearchController in iOS 8.
I have a UISearchBar which I've added to the titleView and a corresponding results controller.
I have it set up so that when the search results are scrolled scrollViewWillBeginDragging(), I call searchBar.resignFirstResponder() to hide the keyboard (same as Spotlight search, Facebook app, etc. -- let's us see the results hidden behind the keyboard).
The problem I have is that I can't seem to be able to re-gain focus on the search bar. It should be as simple as just tapping on the search bar, but this seems to end the search completely, rather than refocusing the search bar to allow the search text to be edited.
I can't figure out why the UISearchBar isn't regaining focus.
Any suggestions?
Turns out after much trial and error, the solution is surprisingly simple:
Just set dimsBackgroundDuringPresentation = false on your UISearchController and it all works fine!

UISearchDisplayController's full-screen background intercepts touch events in iOS 7

I have a UITableview that doesn't take up the whole screen (screenshot). Everything worked fine in iOS 6. But in iOS 7, when the user searches, the search result table takes up the whole view (screenshot).
To fix this, I tried setting the frame manually as described in this answer. The appearance is now correct (screenshot), but now the "<" button in the top left doesn't receive tap events when the search results table is displayed.
It seems the searchResultsTableView is adding a full-screen background view that is intercepting touch events. To prove this, I added this code to didShowSearchResultsTableView:
controller.searchResultsTableView.superview.backgroundColor = [UIColor blueColor];`
This screenshot confirms my hypothesis.
How can I fix this to allow the "<" button to receive tap events?
I want to avoid modifying controller.searchResultsTableView.superview so that my change doesn't break in future versions of iOS.
And what change in iOS 7 caused this behavior to start happening?
I am still searching for a better solution, but currently my solution is in the viewControllers viewDidLayoutSubviews tell your view to move to front. The code would look something like this.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self.view bringSubviewToFront:self.navigationBar];
}

Resources