【问题标题】:Setting the frame size of child UIViews设置子 UIView 的帧大小
【发布时间】:2014-09-21 00:45:29
【问题描述】:

我有一个 UIViewController,我将其称为 parentViewController(它本身位于选项卡视图控制器中,但我认为这与此问题无关)。

我试图让这个 UIViewController 在分屏设置中显示两个子视图。其中一个子视图由UITableViewController 的自定义子类控制(称为tvc。另一个子视图由自定义UIViewController 控制,称为dvc

parentViewControllerinit 方法中,我实例化了两个子视图控制器tvcdvc

parentViewControllerviewDidLoad 方法中,我添加了子视图控制器和子视图。

如果我只添加 tvc 子项,它可以正常工作:表格视图占用所有可用空间,正确旋转等。这就是代码的样子:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _tvc.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self addChildViewController:_tvc];
    [self.view addSubview:_tvc.view];

    NSDictionary *viewDictionary = @{ @"tvc": _tvc.view };
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[tvc]|" options:0 metrics:nil views:viewDictionary];

    [self.view addConstraints:constraints];
}

但是,如果我随后添加 dvc 控制器和子视图,它将停止正常工作。表格视图不显示,只有一个空白的白色视图。这是我添加 dvc 时的样子:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _tvc.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self addChildViewController:_tvc];
    [self.view addSubview:_tvc.view];

    _dvc.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self addChildViewController:_dvc];
    [self.view addSubview:_dvc.view];

    NSDictionary *viewDictionary = @{ @"_tvc": _tvc.view, @"_dvc": _dvc.view };
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[_tvc]-[_dvc]|" options:0 metrics:nil views:viewDictionary];

    [self.view addConstraints:constraints];
}

我没有用框架初始化tvc,而只是让超类UITableViewController 处理它。在dvcloadView 方法中,我使用CGRectZero 框架实例化我的自定义UIView,因为我假设自动布局约束会正确设置大小。

我做错了什么无法并排显示两个子视图?

【问题讨论】:

  • 您是否尝试过为添加的子视图添加约束?
  • 我不确定你的意思。我必须在子视图和父视图上都调用 addConstraints 吗?我认为父视图负责将自动布局约束应用于其子视图?
  • 添加约束后添加断点,然后在控制台输入:po [self.view hasAmbiguousLayout]。这将检查您的约束是否设置正确。未正确设置的约束可能是您的问题。 LE - 如果您的编译器是 lldb,则此方法有效
  • @MihaiAndreiRustiuc 我已经搜索过了,我已经搜索过了,但我终生无法弄清楚在哪里输入该命令!

标签: ios objective-c ipad uiview uiviewcontroller


【解决方案1】:

我也有类似的情况,添加子控制器后没有限制。

我添加了用作子视图控制器容器的视图。这是代码,我用来添加控制器:

[self addChildViewController:controller];
// menu view is container for menu controller subview (in your case tvc)
[self.menuView addSubview:controller.view];
// add constraints
[self pinView:controller.view toSuperView:self.menuView];
[controller didMoveToParentViewController:self.menuController];

以下函数使子视图填充父视图

- (void) pinView:(UIView *) childView toSuperView:(UIView *) superView {
    UIView *addedSubview = childView;
    addedSubview.translatesAutoresizingMaskIntoConstraints = NO;

    [superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[addedSubview]|" options:nil metrics:nil views:NSDictionaryOfVariableBindings(addedSubview)]];
    [superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[addedSubview]|" options:nil metrics:nil views:NSDictionaryOfVariableBindings(addedSubview)]];
}

【讨论】:

  • 所以在你的例子中,我会为两个子视图(tvc.viewdvc.view)在menuView 上调用addSubview?我需要做什么来创建menuView?是否负责实例化tvcdvc?我也不清楚pinView:toSuperView 函数的作用 - 为什么我们不能在将子视图添加到超级视图时添加约束?
【解决方案2】:

试试这个。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _tvc.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self addChildViewController:_tvc];
    [self.view addSubview:_tvc.view];

    _dvc.view.translatesAutoresizingMaskIntoConstraints = NO;
    [self addChildViewController:_dvc];
    [self.view addSubview:_dvc.view];


    // For constraints
    NSMutableArray *allContraints = [[NSMutableArray alloc] init];
    CGFloat halfScreen = [UIScreen mainScreen].bounds.size.height / 2.0;

    NSDictionary *metrics = @{
                              @"viewHeight": @(halfScreen)
                              };

    NSDictionary *viewDictionary = @{ @"tvc": _tvc.view, @"dvc": _dvc.view};
    NSArray *constraintsVerticals = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[tvc(viewHeight)]-[dvc]|" options:0 metrics:metrics views:viewDictionary];
    [allContraints addObjectsFromArray:constraintsVerticals];

    NSArray *constraintsTvcHorz = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[tvc]|" options:0 metrics:nil views:viewDictionary];
    [allContraints addObjectsFromArray:constraintsTvcHorz];

    NSArray *constraintsDvcHorz = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[dvc]|" options:0 metrics:nil views:viewDictionary];
    [allContraints addObjectsFromArray:constraintsDvcHorz];


    [self.view addConstraints:allContraints];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多