0

In my attempt to Keep UISearchBar visible even if user is sliding down, I've:

  • Added a UITableView & UISearchView as subviews of view.

    • tableView's frame = view.bounds
    • searchBar is locked to the bottom of topLayoutGuide with Auto Layout.

But, since even with automaticallyAdjustsScrollViewInsets = true, the searchBar covers the tableView, how do I increase tableView.contentInset.top by 44 (the searchBar's height)?

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651

1 Answers1

2

You mean like this?

UIEdgeInsets insets = tableView.contentInset;
insets.top += 44;
tableView.contentInset = insets;
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51
  • Yeah, but, I'm using Swift, so it's just one line: `tableView.contentInset.top += 44`. But, I couldn't figure out where to do it. So, I just repositioned the `tableView` snapping its top to the `searchBar`'s bottom. That way, the `tableView` is no longer behind the `searchBar`. I'm happy with it. :-) – ma11hew28 Nov 05 '14 at 21:06
  • Maybe it's because I don't know much swift, but wouldn't the exact same lines work? Maybe with an extra `var` or `let` on the initial variable creation line? – Ian MacDonald Nov 05 '14 at 21:10
  • Yep, I just don't know where/when to adjust the insets. – ma11hew28 Nov 05 '14 at 23:36
  • You could do this in your view controller's `viewDidLoad` or `viewWillLayoutSubviews`. – Ian MacDonald Nov 06 '14 at 14:48
  • It doesn't work in `viewDidLoad` because `topLayoutGuide.length` is still `0` then. It might work in `viewWillLayoutSubviews`, but that gets called a lot, so you'd probably need additional logic and/or memory. I tried subclassing `UITableView` and overriding `contentInset` and adding 44 to `conentInset.top` and subtracting 44 from `contentOffset.y` in `didSet`. That worked for the first call to `contentInset` but then broke after subsequent calls. So, I think if I added a boolean property and some logic to only make that adjustment once, that would work. But, I'd rather stick with what I have. – ma11hew28 Nov 06 '14 at 15:57
  • Why does `topLayoutGuide.length` have any effect on your ability to set `.contentInset`? Also, if this is layout that changes based on some condition, it absolutely belongs in `viewWillLayoutSubviews`. If you find `viewWillLayoutSubviews` is being called "a lot", try investigating the reason that it's called so often. Even if it is called often, setting `.contentInset` within it is not going to have any significant impact on performance, and it won't cause any visual glitches. – Ian MacDonald Nov 06 '14 at 16:01
  • Since `automaticallyAdjustsScrollViewInsets = true `, if `topLayoutGuide.length` is `0`, then `tableView.contentInset.top` will also be `0`, then `44` after I add to it, and then `64` after being automatically adjusted. But, I want it to be `64 + 44`. Yeah, adjusting `tableView.contentInset.top` in `viewWillLayoutSubviews` makes sense since `tableView.contentInset.top` is dependent on `topLayoutGuide.length`. Still, I think I'll pass for now because I already implemented the solution in my first comment. But, thanks! :-) – ma11hew28 Nov 06 '14 at 16:14