2017-02-26 74 views
0

我在控制器顶部的视图(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分),然后它也消失了。

+0

这是您的*完整*约束集?如果没有,请张贴其余的 - 他们很重要。 – dfd

+0

他们是,虽然我拿出了程序约束,因为他们没有必要。现在我只有'view.addSubview(addressLabelBackground)'。在故事板中,我在'addressLabelBackground'中放置了'addressLabel'和'logo'。现在,无论方向如何,这些子视图都保留在“addressLabelBackground”中。然而,我仍然无法获得'addressLabelBackground'在横向模式下调整为全角。 – KingTim

+0

然后,您在addressLabelBackground上缺少一些约束。你已经给它一个centerX和一个顶端,但它的内在宽度是多少?你是否添加了文本属性值? – dfd

回答

0

您的addressLabelBackground需要自动布局,不仅钉在顶部,而且钉在它的超级视角的两侧。用以下两个替换现有的widthAnchor约束:

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

我得到了崩溃:''无法激活约束与锚因为他们没有共同的祖先。约束或其锚点是否引用不同视图层次中的项目?这是非法的。' – KingTim

+0

这是你的最大限制。导航栏与addressLabelBackground不在同一层次结构中。这里有两个SO问题可以帮助你展示如何使用topMarginGuide:http://stackoverflow.com/questions/26837957/is-it-possible-to-pin-a-uiviews-top-to-the-bottom-of-the - 导航栏,http://stackoverflow.com/questions/32568056/pin-uiview-just-below-uinavigationbar-using-autolayout – dfd

+0

看到我的编辑,我已经尝试了其他几种方法,这似乎还没有成为可能,以编程方式或在故事板中。绝对令人沮丧 - 我不确定为什么将某些东西固定到导航栏的底部非常困难。 – KingTim

相关问题