2015-01-13 36 views
1

enter image description hereiOS的自動佈局如何伸出子視圖在上海華

正如你可以在截圖中看到這裏是灰色UIView我作爲容器使用。在代碼中,我分配UINavigationController並將其作爲子視圖添加到灰色容器視圖。 UINavigationControllerRootViewController(您可以在左側看到截斷表視圖 - 它是我的UINavigationController的根視圖控制器)。

我的問題是如何將各方面的超級觀點界限。因爲現在我只能看到RootViewController視圖的一部分。

在故事板中,我對灰色容器視圖設置了所有約束,對於RootViewController視圖UINavigationController進行了設置。如果我通常PushViewController而不是添加作爲子視圖它顯示沒有中斷問題。

下面是一個代碼我在UIViewController包含灰色容器視圖:

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    self = [super initWithCoder:aDecoder]; 

    if (self) 
    { 
     KNCouponsViewController *couponsViewControllerByList = [KNInstantiateHelper instantiateViewControllerWithIdentifier:@"KNCouponsViewController"]; 

     self.theNavigationController = [[UINavigationController alloc] initWithRootViewController:couponsViewControllerByList]; 

    } 

    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self.theContainerView addSubview:self.theNavigationController.view]; 
} 

下面是灰色容器視圖約束

enter image description here

這裏是根的視圖約束視圖控制器已切斷表格:

enter image description here

似乎是一個簡單的解決方案是設置UINavigationController查看相同的寬和高爲灰色容器具有:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    CGRect rect = self.theContainerView.frame; 
    rect.origin.x = 0; 
    rect.origin.y = 0; 

    [self.theNavigationController.view setFrame:rect]; 

    [self.theContainerView addSubview:self.theNavigationController.view]; 
} 

然後,它的工作原理是正確的。但是我認爲如果我們不修改一個框架,那麼它會起作用嗎?

回答

0

有兩種方法。

  • 1的方法是調用addSubView代碼viewDidLayoutSubview方法
  • 第二方法是使用下面的代碼設置幀

    [self.theContainerView addSubview:self.theNavigationController.view]; [self.theNavigationController.view setFrame:self.theContainerView.frame];

注:如果您使用第一種方式,然後添加你的代碼

static dispatch_once_t onceToken; 

dispatch_once(&onceToken, ^{ 
[self.theContainerView addSubview:self.theNavigationController.view]; 
[self.containerView layoutIfNeeded]; 
});