0
votes

I want to create a container view which has some views on it. I managed to get this working with Auto Layout. Now I want to place this container in a UINavigationController. I did this in the iOS Designer but the following code doesn't work anymore (it works without navigation controller):

HalfTableViewController testController1 = new HalfTableViewController ();
this.AddChildViewController (testController1);
testController1.View.Frame = View.Bounds;
this.View.AddSubview (testController1.View);

It's in C# but it shouldn't matter if you are not familiar with it. I want to know how I can use the view of the navigation controller as container view on which I can add my views. Currently, I see the navigation bar but the view is a black screen. I tried a few things but I don't know how can I get this working. Your solution can be of course in Objective-C.

Edit:

I tried it without AutoLayout and the table views are showing up. Now I want to know what I'm doing wrong. Here is my Auto Layout code:

View.TranslatesAutoresizingMaskIntoConstraints = false;
tv1.View.TranslatesAutoresizingMaskIntoConstraints = false;
tv2.View.TranslatesAutoresizingMaskIntoConstraints = false;
tv3.View.TranslatesAutoresizingMaskIntoConstraints = false;

var constraints = new List<NSLayoutConstraint> ();
constraints.Add (NSLayoutConstraint.Create (tv1.View, NSLayoutAttribute.Top, NSLayoutRelation.Equal, TopLayoutGuide, NSLayoutAttribute.Bottom, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv1.View, NSLayoutAttribute.Left, NSLayoutRelation.Equal, View, NSLayoutAttribute.Left, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv1.View, NSLayoutAttribute.Right, NSLayoutRelation.Equal, tv2.View, NSLayoutAttribute.Left, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv1.View, NSLayoutAttribute.Width, NSLayoutRelation.Equal, tv2.View, NSLayoutAttribute.Width, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv1.View, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1, 0));

constraints.Add (NSLayoutConstraint.Create (tv2.View, NSLayoutAttribute.Top, NSLayoutRelation.Equal, TopLayoutGuide, NSLayoutAttribute.Bottom, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv2.View, NSLayoutAttribute.Left, NSLayoutRelation.Equal, tv1.View, NSLayoutAttribute.Right, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv2.View, NSLayoutAttribute.Right, NSLayoutRelation.Equal, tv3.View, NSLayoutAttribute.Left, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv2.View, NSLayoutAttribute.Width, NSLayoutRelation.Equal, tv3.View, NSLayoutAttribute.Width, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv2.View, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1, 0));

constraints.Add (NSLayoutConstraint.Create (tv3.View, NSLayoutAttribute.Top, NSLayoutRelation.Equal, TopLayoutGuide, NSLayoutAttribute.Bottom, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv3.View, NSLayoutAttribute.Left, NSLayoutRelation.Equal, tv2.View, NSLayoutAttribute.Right, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv3.View, NSLayoutAttribute.Right, NSLayoutRelation.Equal, View, NSLayoutAttribute.Right, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv3.View, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1, 0));

View.AddConstraints (constraints.ToArray ());

What I'm missing?

2

2 Answers

0
votes

Basically you should know model view controller concept.

  1. Here controller is Navigation controller which contains views which we will to and from.
  2. model is the data
  3. view controller holds view.

Code:

//Create a container view.
From above concept the below is view
TestViewController *testview = [[TestviewCopntroller alloc]init];
//set some properties

//the below is data
testview.somearray = array;
testview.somedictionary = dictionary;

//and this is navigation controller
UINavigationController *nav = [[UInavigationController alloc]initwithrootviewcontroller:testview];

//if you want this as root view then
appdelegate.window.rootviewcontroller = nav;

//if you want to present this view
[self presentviewcontroller:nav animated:YES];

//if you want to push
[self.navigationcontroller pushviewcontroller:testview animated:YES];
0
votes

This seems to do that trick:

//View.TranslatesAutoresizingMaskIntoConstraints = false;
tv1.View.TranslatesAutoresizingMaskIntoConstraints = false;
tv2.View.TranslatesAutoresizingMaskIntoConstraints = false;
tv3.View.TranslatesAutoresizingMaskIntoConstraints = false;

var constraints = new List<NSLayoutConstraint> ();

constraints.Add (NSLayoutConstraint.Create (tv1.View, NSLayoutAttribute.Top, NSLayoutRelation.Equal, TopLayoutGuide, NSLayoutAttribute.Bottom, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv1.View, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, BottomLayoutGuide, NSLayoutAttribute.Top, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv1.View, NSLayoutAttribute.Left, NSLayoutRelation.Equal, View, NSLayoutAttribute.Left, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv1.View, NSLayoutAttribute.Right, NSLayoutRelation.Equal, tv2.View, NSLayoutAttribute.Left, 1, 0));

constraints.Add (NSLayoutConstraint.Create (tv2.View, NSLayoutAttribute.Top, NSLayoutRelation.Equal, TopLayoutGuide, NSLayoutAttribute.Bottom, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv2.View, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, BottomLayoutGuide, NSLayoutAttribute.Top, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv2.View, NSLayoutAttribute.Left, NSLayoutRelation.Equal, tv1.View, NSLayoutAttribute.Right, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv2.View, NSLayoutAttribute.Right, NSLayoutRelation.Equal, tv3.View, NSLayoutAttribute.Left, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv2.View, NSLayoutAttribute.Width, NSLayoutRelation.Equal, tv1.View, NSLayoutAttribute.Width, 1, 0));

constraints.Add (NSLayoutConstraint.Create (tv3.View, NSLayoutAttribute.Top, NSLayoutRelation.Equal, TopLayoutGuide, NSLayoutAttribute.Bottom, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv3.View, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, BottomLayoutGuide, NSLayoutAttribute.Top, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv3.View, NSLayoutAttribute.Left, NSLayoutRelation.Equal, tv2.View, NSLayoutAttribute.Right, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv3.View, NSLayoutAttribute.Right, NSLayoutRelation.Equal, View, NSLayoutAttribute.Right, 1, 0));
constraints.Add (NSLayoutConstraint.Create (tv3.View, NSLayoutAttribute.Width, NSLayoutRelation.Equal, tv1.View, NSLayoutAttribute.Width, 1, 0));