Situation: I've got a UITableViewController loading some data asynchronously from a service. During this time I would like to place a full screen (except navigation bar) view over the table view showing my custom indicator and text.
Problem: The problem I'm facing is that when my custom view (it has a red background) is placed over the UITableView the lines of the table view are shown trough my custom view (see image below).
What I tried: I tried to use insertBelow and above, didn't work. I also tried to do: tableview.Hidden = true, but this also hides the custom view for some reason as seen on image 2.
Image1: For some reason I can see the lines threw my view.
Image 2: Tableview + custom view gone when hidden = true used.
My code:
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
UIView view = new UIView (new RectangleF (0, 0, this.TableView.Frame.Width, this.TableView.Frame.Height));
view.BackgroundColor = UIColor.Red;
this.TableView.AddSubview (view);
TableView.Source = new SessionTableViewSource ();
}
Swift / Storyboard Solution
Note: The code below assumes one has a custom view (ratingView in my case) that is to be presented over a UITableView.
I've read many answers to this and similar questions on SO. The other answers from these sources worked to varying degrees for me (e.g.,view loaded but not shown or not accessible,...). I am using Swift 2.0+ and I am sharing the complete solution for doing this using a UITableViewController.
Create an outlet to the Navigation Bar and the view, which you want to bring over the tableview.
In my case I also wanted to animate the view over the tableview so I used a class variable to hold a reference to the inflection point and a point above the scene (off-screen).
Then in
viewDidLoad
I called a function (configureRatingViewAutoLayout) which configures and adds the constraints for the new view to be animated over the tableview.Sometime later... In response to a UIControl event I call this method.
These helper methods can be configured to control access to elements in your scene during the presentation of the view.
Caveats
Try this to hook a button at bottom of the
UITableViewController
declare button as a variable:
and in
viewDidLoad
:and implement this method:
The issue is that the View of a
UITableViewController
is aUITableView
, so you cannot add subviews to the controller on top of the table.I'd recommend switching from a
UITableViewController
to a simpleUIViewController
that contains aUITableView
. This way the controller main view is a plainUIView
that contains a table, and you can add subviews to the mainUIView
and they will be placed on top of the table view.You can use
self.navigationController.view
as view for adding subview.You can try to add the view to the window instead of nesting it in the table view like this: