gpt4 book ai didi

ios - 如何为 subview Controller 设置 topLayoutGuide 位置

转载 作者:IT王子 更新时间:2023-10-29 07:47:41 24 4
gpt4 key购买 nike

我正在实现一个自定义容器,它与 UINavigationController 非常相似,只是它不包含整个 Controller 堆栈。它有一个 UINavigationBar,它被限制在容器 Controller 的 topLayoutGuide 上,恰好距离顶部 20px,这没关系。

当我添加一个 subview Controller 并将其 View 放入层次结构时,我希望在 IB 中看到它的 topLayoutGuide 并用于布置 subview Controller 的 View 的 subview ,以显示在我的导航栏底部。相关文档中有说明要做什么:

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:

  • A view controller not within a container view controller constrains this property to indicate the bottom of the status bar, if visible,
    or else to indicate the top edge of the view controller's view.
  • A view controller within a container view controller does not set this property's value. Instead, the container view controller constrains the value to indicate:
    • The bottom of the navigation bar, if a navigation bar is visible
    • The bottom of the status bar, if only a status bar is visible
    • The top edge of the view controller’s view, if neither a status bar nor navigation bar is visible

但我不太明白如何“限制它的值”,因为 topLayoutGuide 和它的长度属性都是只读的。

我试过这段代码来添加 subview 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 指定了“Under Top Bars”和“Under Bottom Bars”。其中一个 View 特别受限于顶部布局指南,无论如何在设备上它似乎距离容器导航栏底部 20px...

使用这种行为实现自定义容器 Controller 的正确方法是什么?

最佳答案

经过几个小时的调试,据我所知,布局指南是只读的,并且派生自用于基于约束的布局的私有(private)类。覆盖访问器什么也不做(即使它们被调用),而且这一切都非常烦人。

关于ios - 如何为 subview Controller 设置 topLayoutGuide 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19588171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com