【问题标题】:TopAnchor of viewController changin in iMessage Extension between presentation modes表示模式之间 iMessage Extension 中 viewController 的 TopAnchor 更改
【发布时间】:2017-01-15 10:04:48
【问题描述】:

我正在调查 iMessage 应用程序(是的,我知道)并且在演示模式之间移动时遇到问题。下面的一系列截图显示,当应用程序启动时,在紧凑模式下一切正常。展开后一切仍然正确,但是当我返回压缩时,内容会向下移动,看起来与大消息导航栏的高度相同(我相信是 86)

当切换回紧凑视图时,我尝试将顶部约束设置为 -86,但是,这要么什么都不做,要么将其发送回应该在的位置,然后减去 86,因此它消失得太高了。我已将此项目基于应用程序中的 IceCream 示例项目,因此不确定此问题来自何处(可能是自动布局,但所有内容都固定在布局指南中)

这是添加视图控制器的代码:

func loadTheViewController(controller: UIViewController) {
    // Remove any existing child controllers.
    for child in childViewControllers {
        child.willMove(toParentViewController: nil)
        child.view.removeFromSuperview()
        child.removeFromParentViewController()
    }

    // Embed the new controller.
    addChildViewController(controller)

    controller.view.frame = view.bounds
    controller.view.translatesAutoresizingMaskIntoConstraints = true
    view.addSubview(controller.view)

    controller.view.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    controller.view.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    controller.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    controller.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

    controller.didMove(toParentViewController: self)
}

我一直在为此努力,所以欢迎任何建议。

【问题讨论】:

    标签: ios swift uiviewcontroller imessage-extension


    【解决方案1】:

    您正在设置视图约束,但您已将 translatesAutoresizingMaskIntoConstraints 设置为 true。自动调整掩码约束可能会与您添加的约束冲突,导致意外结果。你应该改为:

    controller.view.translatesAutoresizingMaskIntoConstraints = false
    

    除了固定到view.topAnchor,您还应该固定到topLayoutGuide,这将考虑到顶部导航栏。

    controller.view.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
    

    同样,

    controller.view.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor).isActive = true
    

    【讨论】:

    • 谢谢,已经解决了。我查看了 Apple 的 IceCream 示例应用程序,看起来我已经更改了 translatesAutoresizingMaskIntoConstraints,但是由于某种原因它们固定到了 topAnchor 而不是 layoutguides。
    猜你喜欢
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    相关资源
    最近更新 更多