在视图代码 class 文件中引用超级视图

Referencing superview inside of view code class file

我目前正尝试在 Swift 中以编程方式学习约束和样式。我还试图通过拆分与 "styling" 相关的代码来维护干净和模块化的代码。

我只有 LoginViewController:

import UIKit

class LoginViewController: UIViewController {

    var loginView: LoginView!

    override func viewDidLoad() {
        super.viewDidLoad()

        loginView = LoginView(frame: CGRect.zero)
        self.view.addSubview(loginView)

        // AutoLayout
        loginView.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets.zero, excludingEdge: .bottom)
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

}

然后我的LoginView:

import UIKit

class LoginView: UIView {
    var shouldSetupConstraints = true

    var headerContainerView: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)

        // Header Container View
        headerContainerView = UIView(frame: CGRect.zero)
        headerContainerView.backgroundColor = UIColor(red:0.42, green:0.56, blue:0.14, alpha:1.0) // #6B8E23
        headerContainerView.translatesAutoresizingMaskIntoConstraints = false

        self.addSubview(headerContainerView)

        headerContainerView.topAnchor.constraint(equalTo: self.superview!.topAnchor)

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func updateConstraints() {
        if(shouldSetupConstraints) {
            // AutoLayout constraints
            shouldSetupConstraints = false
        }
        super.updateConstraints()
    }
}

我被卡住的地方只是简单地尝试将这个 headerContainerView 添加到我的 superview 的顶部。我希望能够添加它,使其固定在 superview 的顶部、左侧和右侧,并且只有 superview's 高度的 1/3。我继续尝试引用 superview 但没有成功,而且我找不到可以帮助我在互联网上理解的解决方案。对我如何完成这个有什么建议吗?

感谢您抽出宝贵时间回复我们。

注意:我确实开始使用 PureLayout,这真的很棒。然而,我是一个喜欢了解幕后发生的事情或至少如何在其基础级别编写代码的人。您可以看到我在我的 LoginViewController 中使用了 PureLayout 函数,但我希望改变它。我更喜欢不添加第三方库的解决方案。

headerContainerView.snp.makeConstraints { (make) in
        make.top.equalTo(self)
        make.leading.and.trailing.equalTo(self)
        make.height.equalTo(self.frame.height/3)
    }

SnapKit.

使用 SnapKit,您可以执行以下操作:

override func viewDidLoad() {
    super.viewDidLoad()

    loginView = LoginView(frame: CGRect.zero)
    self.view.addSubview(loginView)

    // AutoLayout
    loginView.snp.makeConstraints { (make) in
        make.left.equalTo(view.snp.left)
        make.right.equalTo(view.snp.right)
        make.top.equalTo(view.snp.top)
        make.height.equalTo(view.snp.height).multipliedBy(1/3)
    }
}

这里自定义UIView中的self class是headerContainerView的父视图所以,你可以加上这个,另外我建议先学习约束没有第3派对图书馆以充分理解这个概念,因为您将从看到冲突和其他事情中学到很多东西,一旦完成,就转向图书馆

override init(frame: CGRect) {
    super.init(frame: frame)

    // Header Container View
    headerContainerView = UIView(frame: CGRect.zero)
    headerContainerView.backgroundColor = UIColor(red:0.42, green:0.56, blue:0.14, alpha:1.0) // #6B8E23
    headerContainerView.translatesAutoresizingMaskIntoConstraints = false

    self.addSubview(headerContainerView)
    headerContainerView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    headerContainerView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
    headerContainerView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
    headerContainerView.heightAnchor.constraintEqualToAnchor(self.heightAnchor, multiplier:1.0/3.0, constant: 0.0).active = true

}

// 登录视图布局

override func viewDidLoad() {
    super.viewDidLoad()

    loginView = LoginView(frame: CGRect.zero)
    self.view.addSubview(loginView)
    loginView.translatesAutoresizingMaskIntoConstraints = false
    loginView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
    loginView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
    loginView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
    loginView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true