我想创建一个容器视图,其上有一些视图。我设法使用Auto Layout。现在我想将此容器放在UINavigationController
中。我在iOS Designer中做过这个,但是下面的代码不再起作用了(它没有导航控制器):
HalfTableViewController testController1 = new HalfTableViewController ();
this.AddChildViewController (testController1);
testController1.View.Frame = View.Bounds;
this.View.AddSubview (testController1.View);
它在C#中,但如果你不熟悉它并不重要。我想知道如何将导航控制器的视图用作容器视图,我可以在其中添加我的视图。目前,我看到导航栏但视图是黑屏。我尝试了一些东西,但我不知道如何才能使这个工作。您的解决方案当然可以使用Objective-C。
修改
我在没有AutoLayout的情况下尝试了它,并且表视图显示出来了。现在我想知道我做错了什么。这是我的自动布局代码:
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 ());
我失踪了什么?
答案 0 :(得分:0)
基本上你应该知道模型视图控制器的概念。
代码:
//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];
答案 1 :(得分:0)
这似乎可以做到这一点:
//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));