作为另一个视图的子视图显示的视图在.Xib文件中具有以下设置。
以下是触摸呈现按钮时视图的显示方式。
以下是视图呈现方式的代码。
-(void)setUpImageSelectorView{
ImageSelectorViewViewController *imageSelectorVC = [[ImageSelectorViewViewController alloc]initWithNibName:@"ImageSelectorViewViewController" bundle:nil];
imageSelectorVC.delegate = self;
imageSelectorVC.view.contentMode = UIViewContentModeScaleAspectFit;
imageSelectorVC.view.clipsToBounds = YES;
imageSelectorVC.imageView.image = img;
[self.view addSubview:imageSelectorVC.view];
}
答案 0 :(得分:1)
两个问题:
这不是添加具有自己的控制器的子视图的正确代码。您应该使用自定义容器调用,例如:
-(void)setUpImageSelectorView{
ImageSelectorViewViewController *imageSelectorVC = [[ImageSelectorViewViewController alloc]initWithNibName:@"ImageSelectorViewViewController" bundle:nil];
[self addChildViewController:imageSelectorVC];
imageSelectorVC.delegate = self;
imageSelectorVC.view.contentMode = UIViewContentModeScaleAspectFit;
imageSelectorVC.view.clipsToBounds = YES;
imageSelectorVC.imageView.image = img;
[self.view addSubview:imageSelectorVC.view];
// you probably should set the `frame` of this view, too
[imageSelectorVC didMoveToParentViewController:self];
}
请注意addChildViewController
和didMoveToParentViewController
来电。
当您要删除它时,请先致电[imageSelectorVC willMoveToParentViewController:nil]
,当您完成删除所有操作后,请致电[self removeChildViewController: imageSelectorVC]
。
请参阅 View Controller编程指南(适用于iOS)中的Creating Custom Container View Controllers。有关为什么所有这些都非常重要的讨论(即,保持视图层次与视图控制器层次结构同步)请参阅WWDC 2011视频Implementing UIViewController Containment。
如果您通过调试器运行应用程序并暂停执行(点击暂停按钮,而不是停止按钮),您可以通过键入
来确认视图的布局(lldb) po [[UIWindow keyWindow] recursiveDescription]
<UIWindow: 0x7fd91b542420; frame = (0 0; 375 667); gestureRecognizers = <NSArray: 0x7fd91b543000>; layer = <UIWindowLayer: 0x7fd91b5412d0>>
| <UIView: 0x7fd91b5442d0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7fd91b5435c0>>
| | <_UILayoutGuide: 0x7fd91b5445e0; frame = (0 0; 0 20); hidden = YES; layer = <CALayer: 0x7fd91b537030>>
| | <_UILayoutGuide: 0x7fd91b544f40; frame = (0 667; 0 0); hidden = YES; layer = <CALayer: 0x7fd91b543850>>
| | <UIButton: 0x7fd91b446d50; frame = (5 285; 70 30); opaque = NO; tag = 12221; layer = <CALayer: 0x7fd91b4d8fb0>>
| | | <UIImageView: 0x7fd91b57aa20; frame = (0 0; 70 30); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7fd91b53b6f0>>
| | | <UIImageView: 0x7fd91b51c040; frame = (0 0; 0 0); clipsToBounds = YES; hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7fd91b539670>>
有时会看到视图,但如果宽度/高度为零,则不会看到它(特别是如果你剪切到边界)。