1

在此处输入图像描述

正如您在屏幕截图中看到的那样,这里是灰色UIView的,我将其用作容器。在代码中,我分配了一个UINavigationController并将它的视图作为子视图添加到灰色容器视图中。有(您可以UINavigationControllerRootViewController左侧看到截断的表格视图 - 它是我的 UINavigationController 的根视图控制器)。

我的问题是如何将所有方面都固定到超级视图边界。因为现在我只能看到RootViewController视图的一部分。

在情节提要中,我已将所有约束设置为灰色容器视图RootViewControllerUINavigationController. 如果我通常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];
}

这是灰色容器视图的约束

在此处输入图像描述

这是已切断表的根视图控制器的视图的约束:

在此处输入图像描述

似乎一个简单的解决方案是将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];
}

然后它工作正常。但我认为,如果我们不只修改框架,它会起作用吗?

4

1 回答 1

0

有两种方法。

  • 一种方法是在方法中调用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];
});
于 2017-03-02T19:00:08.633 回答