Ios 加载后如何从视图访问属性
故事板: 限制条件: 结果: 我试图了解autolayout,以及如何在容器中使用它 当我打开故事板时,我得到了一个默认的ViewController。我在里面放了一个视图容器。然后我添加了一个松散(没有连接到任何东西)的ViewController。我希望将新ViewController中的内容放入容器中 因此,添加的ViewController将放在容器中,它由三个标签组成,我正在使用autolayout 单击故事板中容器视图控制器的黑条,我转到Identity Inspector并将自定义类设置为“ContainerViewController”。然后,我为松散视图控制器将情节提要ID设置为“ChildController” 然后我覆盖Ios 加载后如何从视图访问属性,ios,iphone,objective-c,Ios,Iphone,Objective C,故事板: 限制条件: 结果: 我试图了解autolayout,以及如何在容器中使用它 当我打开故事板时,我得到了一个默认的ViewController。我在里面放了一个视图容器。然后我添加了一个松散(没有连接到任何东西)的ViewController。我希望将新ViewController中的内容放入容器中 因此,添加的ViewController将放在容器中,它由三个标签组成,我正在使用autolayout 单击故事板中容器视图控制器的黑条,我转到Identity Inspector并将
ContainerViewController.m
中的viewDidLoad
:
- (void)viewDidLoad
{
[super viewDidLoad];
UIViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildController"];
[self addChildViewController:child];
[self.view addSubview:child.view];
}
为什么自动布局的约束在放入容器时不起作用?我希望他们会这样做,这样我就可以把这进一步带到UIPageViewController
编辑:
因此,它似乎是帧大小。在将其添加为子视图之前,我需要执行以下操作:
child.view.frame = CGRectMake(0, 0, 265, 370);
现在,我在ViewController.h
中为容器创建了一个出口
@property (weak, nonatomic) IBOutlet UIView *container;
但是。。在ContainerViewController中,当尚未设置此属性时,如何询问该属性的视图控制器(ViewController)
ViewController *parent = (ViewController*)self.presentingViewController;
UIView *container = parent.container;
NSLog(@"ParentView Container Width: %f, Height: %f", container.frame.size.width, container.frame.size.height);
它只给了我零宽度和高度,因为视图还没有加载。稍后加载时,我会得到实际值
NSLog(@"View Controller Container. Width: %f, Height: %f", self.container.frame.size.width, self.container.frame.size.height);
问题:当presentingViewController的属性已准备好/加载时,我如何访问它们?添加子视图控制器时,应如下设置其视图框架
UIViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildController"];
[self addChildViewController:child];
[self.view addSubview:child.view];
//set the view frame
child.view.frame = self.view.bounds;
因此,子视图控制器将调整其视图框架的大小,布局约束将完成它们的工作;)
编辑
将代码放入-(void)viewdilayoutsubviews
中,如下所示:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
UIViewController *child = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildController"];
[self addChildViewController:child];
[self.view addSubview:child.view];
//set the view frame
child.view.frame = self.view.bounds;
}
只需在视图控制器的
viewWillLayoutSubviews
中尝试将NSLog(@“Bounds%f”,self.view.Bounds.size.height)设置为480。但是容器比这个小,看看故事板的图像,第一个ViewController。我猜这是因为在VeidLoad中还没有设置boounds。尝试改用ViewWillLayoutSubview。将上述代码放在-(void)viewdLayoutSubview
中,而不是-(void)viewDidLoad