OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在带有 XIB 的 IB 中使用 UIContainerView? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:17
标题: ios - 如何在带有 XIB 的 IB 中使用 UIContainerView?

在 Xcode 6 中,我尝试在导航 Controller 内托管的 View Controller 内添加容器 View 。但是在对象库中没有容器 View 选项。我没有使用 Storyboard只是一个普通的 xib。容器 View 是否仅与 Storyboard相关。在这种情况下我是否只使用普通的 UIView



Best Answer-推荐答案


UIContainerView 嵌入了一个 subview Controller 。 Interface Builder 期望显示容器 View 嵌入的 subview Controller ——这在 Storyboard中运行良好,但使用 XIB 是不可能的,因为它们只能显示单个 View Controller 。

如果你绝对不能在你的项目中使用 Storyboard(你真的应该,它们很棒!),你最好的选择是使用 UIView 作为占位符并通过代码添加 subview Controller 。

这是我在 UIViewController 上使用的一个方便的类别,用于处理将 subview Controller 添加到占位符 View 的所有细节。

- (void)addContentControllerUIViewController*)viewController inViewUIView *)inView
{
    viewController.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self addChildViewController:viewController];
    [inView addSubview:viewController.view];
    NSDictionary *viewsDict = @{@"presentedView" : viewController.view};
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat"H:|[presentedView]|" options:0 metrics:nil views:viewsDict];
    [inView addConstraints:constraints];
    constraints = [NSLayoutConstraint constraintsWithVisualFormat"V:|[presentedView]|" options:0 metrics:nil views:viewsDict];
    [inView addConstraints:constraints];
    [viewController didMoveToParentViewController:self];
}

关于ios - 如何在带有 XIB 的 IB 中使用 UIContainerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27386111/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4