change position and layout of table view and table view cell for iOS app in Swift - ios

I have created a table view controller with a table view cell for my iOS app with storyboard in Xcode 6. In the cell I have an image and a label. At the top of the view controller I want to have a header and at the bottom I have a tab bar. That's my setup.
But now I want to change the position and the layout of the table view and the inherit cells, default they cover the whole screen. The table view should start below the header and should end above the tab bar. At the moment the cell content is displayed under the headline and the tab bar. Is it possible to set the positions of the table view and/or cell? And also I want to change the layout of the cells. I want to have some space between two cells and a corner radius.
THX!

This is an old "problem". If you want to use a UITableViewController, the table view will cover the whole screen. In fact the UIViewController's view property will be the table view.
If you want to have a table view and other views on the screen, you will have to use UIViewController and add two subviews to it's view: the header and the table view.
Do the following (which the UITableViewController would have done for you):
add the table view as an ivar;
have the class conform to UITableViewDelegate and UITableViewDataSource and
set it to the table view's delegate and datasource

Related

How to make the cells inside of a tableview begin lower down?

I have a tableview. Right now the first cell will begin right at the top of the tableview, but I want it to start some number below the top of the tableview so that I can addSubview() in that spot without covering content in the first cell.
Below you can see what I want to achieve.
The rectangle in the center represents the cells, the line at the top represents the top most of the tableview.
Short answer: You don't. The contents of a table view belong to the table view. You should not try to add subviews to a table view.
As #rickramirr says, you either need to position your table view lower on the screen, or use a header view that contains the contents you want.
(If you decide to position your table view lower down, need to either not manage your table view with a UITableViewController, or have a UITableViewController as a child view controller of the screen's main view controller. A UITableViewController is kind of dumb, and only knows how to manage a table view and nothing else.)
I think that maybe you should add a custom header to your table view or put another view above your table view. Here is Apple docs to customize your header: https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections
All the views that you want to insert between one tableView cell and another must extend UITableViewCells too. You can create custom cells by defining a new xib file containing a UITableViewCell, whose content could be whatever view you want. Then, you must create a swift file that extends UITableViewCell associated with the xib file. Finally you need to record that cell into the table, either programmatically or by manually inserting it into the storyboard within the table. At that point you will decide its position using the UITableViewDelegate "cellForRowAt" method.

Table view header, storyboard, Xcode 11?

In XCode 11,
new storyboard,
add a UIViewController (sic: NOT a table view controller),
hit the + symbol top right,
drag a table view into the view controller.
Perhaps add four constraints so it is simply full-screen.
Table view attributes inspector, perhaps set Prototype Cells to not-zero, say 2.
How do you now add a header view to the table view?
(The header view should (obviously) be able to handle dynamic height content.)
The easy way of to achieve adding HeaderView to UITableView is.
Plus + button from Top Bar and Select an UIView
Then in hierarchy add the exact bottom of the UITableView.
See the image.
Your UIView element must be like that in the hierarchy.
Then the UIView calculations for dynamic view upon to you.
(You can create an IBOutlet of the this UIView and do some calculation in your UIViewController. )

Adding a fixed-position view on top of a view

I have a UITableViewController inside a UINavigationController. I'm adding a "modal" subview to the tableView, which is a custom UIView when one of the rows is selected.
(It’s modal in spirit, not in the UIKit sense, since Apple doesn’t support modal views on iPhone, I’m adding it with a [self.view addSubview:customView] in my table view’s controller.)
I would like it to appear at the bottom of the screen and stay put there. I can get it to draw at the bottom, but once I scroll the table view, the view moves with it. Here are some illustrations:
Initial position (good):
Position after scrolling (bad):
I'm getting the bottom position by subtracting the height of all the chrome (navigation bar and status bar) as well as the height of the custom view from [UIScreen mainScreen].bounds.
How can I get the custom view to stay put? Should I constantly be adjusting its frame when the table view is being scrolled?
Your best and most flexible option is to switch to using a view controller with the table view as a subview so that you can change its frame and add sibling views. Then, when you want to add a modal you can run an animation to move the table view out of the way and slide the modal view in.
UITableViewController.view is an instance of UITableView. Means you added your custom view into a scroll view and that is why it scrolls. You can try to put your custom view into the tableFooterView property of the UITableView, which is the old school solution.
Personally I would create a container UIViewController and have UITableViewController be a sub viewController of it. Another sub viewController UIViewController or just a simple UIView could represent the footer.

How to add buttons and search bar into header at top of UITableViewController?

I have a search bar and some buttons in a view in my UITableViewController, but I don't know how to make that view the header? I've attached the referencing outlet to the UITableController. But currently the search bar and buttons scroll up and down with the tableview, but I want them to be the header so they stay fixed.
Here's a screen shot of what I currently have...
ok I ctrl/dragged from the view to my ViewController and created a IBOutlet, as you can see in this next image, and I added in the line to set it to be the header but it seems to give an error??
The easiest solution would be to move that view containing the search bar and buttons to being a subview of the view controller's view instead of inside the table view. Then apply your constraints so that it is pinned to the top and the table view is constrained to the bottom of the view.
If the list of elements you are displaying do not have more than one section, then you could implement the tableView:viewForHeaderInSection: delegate method. Create the headerview containing the searchbar in storyboard and return the instance. It will be always on top of the list.
Apple Documentaion

Container View or UIViewController for tableView with SubView

I have a UIViewController in storyboard that will be a sliding up menu. I want to place it in a UITableView. If I place it as subview of UITableView and prevent it from scrolling, it gets the look I want, with one problem, the section headers of tableview scroll above subview.
Now, in order to prevent this behavior, I could reduce tableview frame, but it doesn't work because my subview is inside that same frame, right?
So , if that is true, what is the best way to achieve this? Place both screens inside a third UIViewController?
Don't try to put other views inside a table view. Bad news.
If your table view is managed by a UITableViewController then that's really all you can have in the table view (unless your views are inside cells, or in headers or footers.)
If you ARE using table view controllers to manage your table view(s), I suggest you create a container view on the view controller that needs to contain the table view, then control-drag an embed segue to the table view controller. That way the table view controller becomes a child view controller of the main VC, and you can put other view elements on the screen to your heart's content.

Resources