Reputation: 97
I'm trying to put a UIView (banner) on top of the list (tableView), so the UIview will not disappear when the user scrolls down the list. I tried this code, but didn't work.
override func viewDidLayoutSubviews() {
self.view.addSubview(banner)
banner.frame.size.width = self.view.frame.size.width
}
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
var rect = self.banner.frame
rect.origin.y = max(0,scrollView.contentOffset.y + scrollView.contentInset.top)
self.banner.frame = rect
}
Any advice how to fix it? Thank you
Upvotes: 0
Views: 570
Reputation: 14666
Instead of trying to keep the view within the scrollView
, a much easier method would be to use a standard UIViewController
with a UIView
attached to the top using AutoLayout
with constraints
and UITableView
attached to the bottom of the UIView
and the bottom of the UIViewController
.
With this layout, only your smaller tableView
would be scrolling and your UIView
would be stationary in place, outside the scope of the scrolling UITableViewCells
.
If you are currently using an UITableViewController
, you'll need to remove the override
modifiers from your tableView
methods, make your UIViewController
implement UITableViewDataSource
and UITableViewDelegate
, then attach the delegates
either in code or storyboard.
Your new UIViewController
would look like this at the top:
class MyController: UIViewController, UITableViewDataSource, UITableViewDelegate {
...
}
Upvotes: 2