swift - 如何使我的内容位于导航 Controller 下方,而不以编程方式调整内容 y 位置

标签 swift

如何使我的内容位于导航 Controller 下方,而无需以编程方式调整内容 y 位置。我问这个问题的原因是因为当我使用 y 位置调整内容时,它看起来很好,但是当设备旋转时,我会得到一个白色间隙,因为导航栏高度发生了变化。

This is an image of adjust view  y position portrait enter image description here

我的代码

import UIKit

class ProfileViewController: UINavigationController {


    let topProfileView = UIView()

    override func viewDidLoad() {

    view.backgroundColor = .white

        topProfileView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(topProfileView)
        topProfileView.backgroundColor = .red
        topProfileView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
        topProfileView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
        topProfileView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
        topProfileView.heightAnchor.constraint(equalToConstant: 100).isActive = true
        topProfileView.topAnchor.constraint(equalTo: view.topAnchor, constant:44).isActive = true

    }

最佳答案

您可以使用自动布局来做到这一点。 自动布局中红色 View 的x和y位置为0

关于swift - 如何使我的内容位于导航 Controller 下方,而不以编程方式调整内容 y 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39764926/

相关文章:

swift - 防止 View 在 NavigationView 中弹出

Swift '无法使具有标识符 intervalCellIdentifier 的单元格出队

json - 如何 native 访问 Swift 4.2 中的不同 JSON key ?

Swift 3,交换两个表格单元格

ios - 如何通过 Google 登录使用 Google Apps (iOS 7)

ios - 将尝试通过打破约束来恢复(但不确定为什么我的约束是错误的)

ios - nil 是初始化程序中唯一允许的返回值吗?

ios - PDF 文件无法在 UIDocumentInteractionController 中打开

swift - 在 DDMathParser (Swift) 中定义自定义函数

ios - Xcode 6 playground — 我无法创建 OS X playground 文件