self.perform segue 导致视图不一致

self.perform segue causes view to not conform

我已经以编程方式创建了一个带有内容视图的视图布局,当这个控制器是初始视图控制器时,我得到了这样的东西。

:

当我有另一个视图控制器并转到这个 viewcontroller 时,我得到这样的结果:

附上我的观点Class:

class TestView: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
    setupViews()
    setupConstraints()
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
func setupViews() {
    self.addSubview(contentView)
}
func setupConstraints() {
    self.translatesAutoresizingMaskIntoConstraints = false
    contentView.translatesAutoresizingMaskIntoConstraints = false
    contentView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
    // check what safearealayoutguide is
    if #available(iOS 11.0, *) {
        print("IOS11 Screen")
        contentView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
    } else {
        contentView.topAnchor.constraint(equalTo: self.layoutMarginsGuide.topAnchor, constant: 0).isActive = true
    }
    contentView.rightAnchor.constraint(equalTo: self.layoutMarginsGuide.rightAnchor, constant: 0).isActive = true
    if #available(iOS 11.0, *) {
        contentView.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
    } else {
        contentView.bottomAnchor.constraint(equalTo: self.layoutMarginsGuide.bottomAnchor, constant: 0).isActive = true
    }
}
let contentView: UIView = {
    let view = UIView(frame: CGRect(x: 100, y: 100, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width))
    //view.layer.backgroundColor = CGColortran
    view.layer.borderWidth = 1
    view.layer.borderColor = UIColor.green.cgColor
    return view
}()
}

然后我的视图控制器:

class TestViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
override func loadView() {
    self.view = TestView(frame: UIScreen.main.bounds)
}
}

当这是应用程序中的唯一屏幕时,它可以工作。当我添加另一个视图控制器并执行 self.performSegue(withIdentifier: "LoginUser", sender: nil) 时,它似乎破坏了视图。这是怎么回事?

评论

self.translatesAutoresizingMaskIntoConstraints = false

因为目前您混合设置框架 TestView(frame: UIScreen.main.bounds) 和约束,上面的行仅在您设置约束时用于视图,因为它会使框架无效