Arnav GUPTA
Arnav GUPTA

Reputation: 325

How to add an image above tableview in TableViewController - Swift 3

I have a tableview in a tableview controller with a search bar. I want to add an image to the top of it in such a way, that it acts like a navigation bar and doesn't move and essentially shrinks the tableview. I usually use a view controller and add an image to the top, but I can't seem to do the tableviewcontroller. I tried adding a UIView and calling the

tableViewHeader  

method, however my search controller disappears and when I scroll down in the tableview, the header disappears and the tableview covers the whole page.

Is there any way of adding sort of a sticky header to the tableview in the tableviewcontroller, so that it stays there and my search controller doesn't disappear.

Upvotes: 1

Views: 1610

Answers (2)

Amir Khan
Amir Khan

Reputation: 1318

Add UIImgaeView in HeaderView of that TableView. Something like that -

    let headerImageView = UIImageView.init(frame: Frame)

    headerImageView.image = Image

    self.tableView.tableHeaderView = headerImageView

Upvotes: 3

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16446

No With UITableViewController You can't add image like you can do with UIViewController

For now you have following options

1) Switch to UIViewController and add uitableview and whatever your view is.

2) If you are using navigation controller then add subview to navigation controlller's view

3) Add Subview directly to UIWindow of appdelegate

Upvotes: 0

Related Questions