我在控制器顶部的视图(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分),然后它也消失了。
这是您的*完整*约束集?如果没有,请张贴其余的 - 他们很重要。 – dfd
他们是,虽然我拿出了程序约束,因为他们没有必要。现在我只有'view.addSubview(addressLabelBackground)'。在故事板中,我在'addressLabelBackground'中放置了'addressLabel'和'logo'。现在,无论方向如何,这些子视图都保留在“addressLabelBackground”中。然而,我仍然无法获得'addressLabelBackground'在横向模式下调整为全角。 – KingTim
然后,您在addressLabelBackground上缺少一些约束。你已经给它一个centerX和一个顶端,但它的内在宽度是多少?你是否添加了文本属性值? – dfd