2016-09-19 60 views
1

通過「奇怪」,我注意到以下行爲:添加ChildViewController引起奇怪的行爲對孩子的ViewController

  • 背景顏色由ChildViewController設置不會出現
  • 儘管增加了基本的點觸手勢識別到ChildViewController,沒有抽頭識別
  • 添加UIButton到示於UIButton的ChildViewController的結果,但存在對的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]; 

這裏有一些很基本的東西我的viewDidLoadChildViewController做:

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]; 

我保證一切都正確連接 - 但是,當我在模擬器或設備上點擊它時,tappedOnViewtappedOnTap中沒有確認。

我缺少一些基本的東西介紹ChildViewController

編輯

對於那些好奇,這個非常基本的應用程序是Github

+0

也許嘗試移動它了'viewDidLoad'改爲'viewDidAppear',看看會發生什麼 – random

+0

奇怪的是,將它移動到'viewDidAppear'會導致一切消失在一起。 – daspianist

+0

嘗試刪除此行[viewController didMoveToParentViewController:self];並做到這一點[自我addChildViewController:viewController]; [self.view addSubview:tapButton]後; –

回答

1

收容電話很好。但是,如果您使用視圖調試器(view debugger),則會看到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] 
]]; 
+1

哇,這個簡單的事情做到了。所有以前的問題已被解決,因爲將其設置爲「true」。感謝您對此的瞭解,並提供設置約束的代碼。 – daspianist

0

試試這個:

ChildViewController *viewController; 
viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ChildViewController"]; 

viewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleheight; 

[self addChildViewController:viewController]; 
[self.view addSubview:viewController.view]; 
[viewController didMoveToParentViewController:self]; 

一般除了那些基本的方法添加一個視圖控制器到另一個時,你不需要做任何事情。因爲視圖會自動調整,你不需要設置所有這些屬性,如大小和中心等...