Search code examples
iosswiftiphonexcodetableview

In a table view controller when adding a container view, how can I move it to the very bottom? (Swift)


I'm currently working on a texting app and to do that I created a table view controller. I want to add a textfield the very bottom of the table view controller. Xcode wouldn't let me add a text field in the controller so I added a container view where I was able to put a textfield inside.

The only issue I have now is placing that container view to the very bottom where the user is able to type in a text (which is typical in any texting app).


Solution

  • The short answer is that you can't add anything to a table view controller. It is a special purpose view controller that manages a table view controller and nothing else. I've always thought this was kind of silly, but it's true.

    What you should do is to start with a custom subclass of UIViewController as your parent view controller. Add a container view to that, and drag an embed segue to a table view controller. Your view controller now contains a table view controller.

    Now add other text fields, buttons, or other view components to the parent view controller.

    Edit

    As SmartCat pointed out in their answer, you could also manage a table view directly in your view controller. There is nothing saying that you must use a UITableViewController.