【问题标题】:View and its subviews disappearing after adding constraints添加约束后视图及其子视图消失
【发布时间】:2017-07-17 11:25:02
【问题描述】:

我在控制器顶部导航栏下方有一个视图 (addressLabelBackground)。它在纵向模式下显示得很好,但是当我将设备切换到横向模式时,我试图让它重新调整大小。

所以我尝试通过将 addressLabelBackground 固定在视图左侧 (0)、视图右侧 (0) 和导航栏顶部 (0) 来在情节提要中添加一些约束,但是当我运行应用程序,视图及其所有子视图都会消失。

我也尝试以编程方式添加约束(就像我为子视图所做的那样),但同样,addressLabelBackground 和其中的子视图消失了。

这是代码(viewDidLoad): 回答后更新,崩溃了

    view.addSubview(addressLabelBackground)
    addressLabelBackground.addSubview(addressLabel)
    addressLabelBackground.addSubview(turnToTechLogo)

    addressLabelBackground.translatesAutoresizingMaskIntoConstraints = false

    addressLabelBackground.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    addressLabelBackground.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    addressLabelBackground.topAnchor.constraint(equalTo: (navigationController?.navigationBar.bottomAnchor)!).isActive = true

    addressLabel.font = UIFont.boldSystemFont(ofSize: 16.0)
    addressLabel.translatesAutoresizingMaskIntoConstraints = false

    addressLabel.centerXAnchor.constraint(equalTo: addressLabelBackground.centerXAnchor).isActive = true
    addressLabel.topAnchor.constraint(equalTo: addressLabelBackground.topAnchor).isActive = true
    addressLabel.bottomAnchor.constraint(equalTo: addressLabelBackground.bottomAnchor).isActive = true

addressLabel 的约束在纵向和横向上都可以正常工作 - 它在视图中保持居中。但是,当我尝试以相同的方式或在情节提要中添加 addressLabelBackground 的约束时,一切都消失了。

当设备处于横向模式时,如何让此视图保持固定在导航栏下方,并拉伸其宽度以适应屏幕宽度?

编辑(第二天):仍在尝试解决这个问题。我尝试制作一个名为 innerContainer 的子视图并将其固定到它的 topAnchor:

    let frame = CGRect(x: 0, y: 20, width: self.view.bounds.width, height: self.view.bounds.height-20)
    let innerContainer = UIView(frame: frame)

    self.view.addSubview(innerContainer)
    innerContainer.addSubview(addressLabelBackground)

    addressLabelBackground.translatesAutoresizingMaskIntoConstraints = false
    addressLabelBackground.topAnchor.constraint(equalTo: innerContainer.topAnchor).isActive = true

但它又一次消失了。

我也尝试将它固定到故事板中 topLayoutGuide 的顶部(0 点),然后它也消失了。

【问题讨论】:

  • 这是您的完整组约束吗?如果没有,请发布其余部分 - 它们很重要。
  • 它们是,尽管我去掉了程序约束,因为它们不是必需的。现在我只有view.addSubview(addressLabelBackground)。在情节提要中,我将addressLabellogo 放在addressLabelBackground 中。现在,无论方向如何,这些子视图都保留在 addressLabelBackground 内。但是我仍然无法让addressLabelBackground 在横向模式下调整为全宽
  • 那么您在 addressLabelBackground 上缺少一些约束。你给了它一个 centerX 和一个 top,但它的内在宽度是多少?是否添加了文本属性值?
  • 我不确定您对文本属性值的含义。问题似乎是,如果我给它任何约束,它(连同子视图)就会消失。你的意思是如果我添加某些约束,它会解决问题吗?
  • 到目前为止,我对任何事情都没有任何限制,并且徽标和标签保持在addressLabelBackground 内所需的位置。

标签: ios swift autolayout


【解决方案1】:

您的addressLabelBackground 需要一个自动布局,不仅要固定在顶部,还要固定在其超级视图的侧面。用这两个替换现有的widthAnchor 约束:

addressLabelBackground.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
addressLabelBackground.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true

【讨论】:

  • 我遇到了崩溃:'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x60000046cd00 "UIView:0x7fdcd460ce40.top"> and <NSLayoutYAxisAnchor:0x60000046cd80 "UINavigationBar:0x7fdcd4605900.bottom"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
  • 这是你的首要约束。导航栏与 addressLabelBackground 不在同一层次结构中。这里有两个 SO 问题有助于展示如何使用 topMarginGuide:stackoverflow.com/questions/26837957/…stackoverflow.com/questions/32568056/…
  • 查看我的编辑,我尝试了其他几种方法,但这似乎仍然不可能,以编程方式或在情节提要中。绝对令人沮丧 - 我不知道为什么将某些东西固定在导航栏的底部如此困难。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多