如何为孩子视图控制器topLayoutGuide位置 [英] How to set topLayoutGuide position for child view controller

查看:222
本文介绍了如何为孩子视图控制器topLayoutGuide位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现自定义的容器,它是类似的UINavigationController除了不成立,整个控制器堆叠pretty。它具有一个限定在容器控制器的topLayoutGuide,这恰好是20像素关闭顶部,这是确定一个UINavigationBar的。

I'm implementing a custom container which is pretty similar to UINavigationController except for it does not hold the whole controller stack. It has a UINavigationBar which is constrained to the container controller's topLayoutGuide, which happens to be 20px off the top, which is OK.

当我添加子视图控制器,并把它认为成我想要的topLayoutGuide在IB看到和用于铺设了子视图控制器的视图的子视图出现在我的导航栏底部的层次。还有一个值得注意的是什么的相关文件中完成:

When I add a child view controller and put its view into the hierarchy I want its topLayoutGuide seen in IB and used for laying out the child view controller's view's subviews to appear at the bottom of my navigation bar. There is a note of what is to be done in the relevant documentation:

该属性的值,具体地讲,所述长度值
  当你查询此属性的对象的属性返回。这个
  值是由两种视图控制器或它的外围约束
  容器视图控制器(如导航或标签栏
  控制器),如下所示:

The value of this property is, specifically, the value of the length property of the object returned when you query this property. This value is constrained by either the view controller or by its enclosing container view controller (such as a navigation or tab bar controller), as follows:


      
  • 的容器视图控制器内的视图控制器不限制这个属性来指示状态栏的底部,如果可见,结果
      否则,指示视图控制器的视图的顶部边缘。

  •   
  • 的容器视图控制器内的视图控制器不设置这个属性的值。相反,容器视图控制器
      限制指示值:
      

        
    • 导航栏的底部,如果导航栏是可见

    •   
    • 状态栏的底部,如果只是状态栏可见

    •   
    • 视图控制器的视图的顶部边缘,如果没有一个状态栏,也不导航栏是可见

    •   

    但我不太明白如何约束它的价值,因为无论是topLayoutGuide,它的长度属性是只读的。

    But I don't quite understand how to "constrain it's value" since both the topLayoutGuide and it's length properties are readonly.

    我已经试过这code添加子视图控制器:

    I've tried this code for adding a child view controller:

    [self addChildViewController:gamePhaseController];
    UIView *gamePhaseControllerView = gamePhaseController.view;
    gamePhaseControllerView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentContainer addSubview:gamePhaseControllerView];
    
    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[gamePhaseControllerView]-0-|"
                                                                             options:0
                                                                             metrics:nil
                                                                               views:NSDictionaryOfVariableBindings(gamePhaseControllerView)];
    
    NSLayoutConstraint *topLayoutGuideConstraint = [NSLayoutConstraint constraintWithItem:gamePhaseController.topLayoutGuide
                                                                                attribute:NSLayoutAttributeTop
                                                                                relatedBy:NSLayoutRelationEqual
                                                                                   toItem:self.navigationBar
                                                                                attribute:NSLayoutAttributeBottom
                                                                               multiplier:1 constant:0];
    NSLayoutConstraint *bottomLayoutGuideConstraint = [NSLayoutConstraint constraintWithItem:gamePhaseController.bottomLayoutGuide
                                                                                   attribute:NSLayoutAttributeBottom
                                                                                   relatedBy:NSLayoutRelationEqual
                                                                                      toItem:self.bottomLayoutGuide
                                                                                   attribute:NSLayoutAttributeTop
                                                                                  multiplier:1 constant:0];
    [self.view addConstraint:topLayoutGuideConstraint];
    [self.view addConstraint:bottomLayoutGuideConstraint];
    [self.contentContainer addConstraints:horizontalConstraints];
    [gamePhaseController didMoveToParentViewController:self];
    
    _contentController = gamePhaseController;
    

    在IB我指定根据热门酒吧和在底部的酒吧为gamePhaseController。其中一个观点是专门约束顶端布局引导,反正在设备上这似乎是20像素从容器的导航栏的底部...

    In the IB I specify "Under Top Bars" and "Under Bottom Bars" for the gamePhaseController. One of the views is specifically constrained to the top layout guide, anyway on the device it appears to be 20px off the the bottom of the container's navigation bar...

    这有什么行为,实现自定义容器控制器的正确方法吗?

    What is the right way of implementing a custom container controller with this behavior?

    推荐答案

    据我已经能够调试小时后告诉,布局指南是只读,并且从用于基于约束的布局私有类的。重写访问不做任何事情(即使他们被称为),这一切都只是craptastically讨厌。

    As far as I have been able to tell after hours of debugging, the layout guides are readonly, and derived from the private classes used for constraints based layout. Overriding the accessors does nothing (even though they are called), and it's all just craptastically annoying.

    这篇关于如何为孩子视图控制器topLayoutGuide位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆