添加 ChildViewController 会导致 child viewcontroller 出现奇怪的行为
Adding a ChildViewController causes strange behavior to the child viewcontroller
"strange",我注意到以下行为:
ChildViewController
设置的背景颜色没有出现
- 尽管向
ChildViewController
添加了基本的点击手势识别器,但没有点击识别
- 向 ChildViewController 添加
UIButton
结果显示 UIButton
,但点击 UIButton 部分时没有响应
在我的 ParentViewController
中,我非常笼统地呈现 ChildViewController
,如下所示:
UIViewController *viewController;
viewController = (ChildViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"];
[self addChildViewController:viewController];
viewController.view.frame = self.view.bounds;
viewController.view.translatesAutoresizingMaskIntoConstraints = NO;
viewController.view.center = self.view.center;
[self.view addSubview:viewController.view];
[self.view bringSubviewToFront:viewController.view];
[viewController didMoveToParentViewController:self];
下面是我在 ChildViewController
的 viewDidLoad
:
中所做的一些非常基本的事情
self.view.backgroundColor = [UIColor yellowColor];
UIButton *tapButton = [UIButton buttonWithType:UIButtonTypeSystem];
[tapButton addTarget:self action:@selector(tappedOnTap) forControlEvents:UIControlEventTouchUpInside];
//button view customization code omitted for brevity
tapButton.userInteractionEnabled = YES;
tapButton.enabled = YES;
[self.view addSubview:tapButton];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOnView)];
[self.view addGestureRecognizer:tapGesture];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:tapGesture];
我确保一切都正确连接 - 但是,当我在模拟器或设备中点击它时,tappedOnView
和 tappedOnTap
中没有确认。
我是否遗漏了一些关于呈现 ChildViewController
的基本知识?
编辑
对于那些好奇的人,这个非常基本的应用程序在 Github。
试试这个:
ChildViewController *viewController;
viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"];
viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleheight;
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
Usually you don't need to do anything when adding a ViewController to another except those basic methods. since the views will be adjusted automatically and you don't need to set all those properties like size and center etc...
围堵电话没问题。但是,如果您使用视图调试器 (),您会发现 frame
不正确。这是因为您已关闭 translatesAutoresizingMaskIntoConstraints
,但您没有任何限制。您可以重新打开它(并相应地设置 autoresizingMask
):
viewController.view.translatesAutoresizingMaskIntoConstraints = true;
或者您可以定义约束而不是设置 frame
(并冗余设置 center
):
//viewController.view.frame = self.view.bounds;
viewController.view.translatesAutoresizingMaskIntoConstraints = false;
//viewController.view.center = self.view.center;
[self.view addSubview:viewController.view];
[NSLayoutConstraint activateConstraints:@[
[viewController.view.topAnchor constraintEqualToAnchor:self.view.topAnchor],
[viewController.view.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
[viewController.view.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
[viewController.view.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor]
]];
"strange",我注意到以下行为:
ChildViewController
设置的背景颜色没有出现- 尽管向
ChildViewController
添加了基本的点击手势识别器,但没有点击识别 - 向 ChildViewController 添加
UIButton
结果显示UIButton
,但点击 UIButton 部分时没有响应
在我的 ParentViewController
中,我非常笼统地呈现 ChildViewController
,如下所示:
UIViewController *viewController;
viewController = (ChildViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"];
[self addChildViewController:viewController];
viewController.view.frame = self.view.bounds;
viewController.view.translatesAutoresizingMaskIntoConstraints = NO;
viewController.view.center = self.view.center;
[self.view addSubview:viewController.view];
[self.view bringSubviewToFront:viewController.view];
[viewController didMoveToParentViewController:self];
下面是我在 ChildViewController
的 viewDidLoad
:
self.view.backgroundColor = [UIColor yellowColor];
UIButton *tapButton = [UIButton buttonWithType:UIButtonTypeSystem];
[tapButton addTarget:self action:@selector(tappedOnTap) forControlEvents:UIControlEventTouchUpInside];
//button view customization code omitted for brevity
tapButton.userInteractionEnabled = YES;
tapButton.enabled = YES;
[self.view addSubview:tapButton];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOnView)];
[self.view addGestureRecognizer:tapGesture];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:tapGesture];
我确保一切都正确连接 - 但是,当我在模拟器或设备中点击它时,tappedOnView
和 tappedOnTap
中没有确认。
我是否遗漏了一些关于呈现 ChildViewController
的基本知识?
编辑
对于那些好奇的人,这个非常基本的应用程序在 Github。
试试这个:
ChildViewController *viewController;
viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"];
viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleheight;
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
Usually you don't need to do anything when adding a ViewController to another except those basic methods. since the views will be adjusted automatically and you don't need to set all those properties like size and center etc...
围堵电话没问题。但是,如果您使用视图调试器 (frame
不正确。这是因为您已关闭 translatesAutoresizingMaskIntoConstraints
,但您没有任何限制。您可以重新打开它(并相应地设置 autoresizingMask
):
viewController.view.translatesAutoresizingMaskIntoConstraints = true;
或者您可以定义约束而不是设置 frame
(并冗余设置 center
):
//viewController.view.frame = self.view.bounds;
viewController.view.translatesAutoresizingMaskIntoConstraints = false;
//viewController.view.center = self.view.center;
[self.view addSubview:viewController.view];
[NSLayoutConstraint activateConstraints:@[
[viewController.view.topAnchor constraintEqualToAnchor:self.view.topAnchor],
[viewController.view.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
[viewController.view.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
[viewController.view.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor]
]];