简体   繁体   English

迅捷/重构

[英]Swift / Refactoring

I'm creating a view to use it as a subview within the viewDidLoad. 我正在创建一个视图,以将其用作viewDidLoad中的子视图。

    override func viewDidLoad() {
    let backgroundView = UIView()
    backgroundView.translatesAutoresizingMaskIntoConstraints = false
    backgroundView.backgroundColor = UIColor.blackColor()

    let viewsDictionary = ["backgroundView":backgroundView]

    let backgroundView_constraint_H = NSLayoutConstraint.constraintsWithVisualFormat(
        "H:[backgroundView(10000)]",
        options: NSLayoutFormatOptions(rawValue: 0),
        metrics: nil, views: viewsDictionary)
    let backgroundView_constraint_V = NSLayoutConstraint.constraintsWithVisualFormat(
        "V:[backgroundView(10000)]",
        options: NSLayoutFormatOptions(rawValue:0),
        metrics: nil, views: viewsDictionary)

    backgroundView.addConstraints(backgroundView_constraint_H)
    backgroundView.addConstraints(backgroundView_constraint_V)

    backgroundView.alpha = 0.9

    self.tableView.backgroundView = backgroundView
}

What's the correct workaround to put all the code into an own .swift file and only call self.tableView.backgroundView = backgroundView at the viewDidLoad? 什么是将所有代码放入一个单独的.swift文件并仅在viewDidLoad处调用self.tableView.backgroundView = backgroundView的正确解决方法?

Help is very appreciated. 非常感谢您的帮助。

        class func backgroundView()->UIView {
            let backgroundView = UIView()
            backgroundView.translatesAutoresizingMaskIntoConstraints = false
            backgroundView.backgroundColor = UIColor.blackColor()
            backgroundView.frame = self.tableView.frame
            backgroundView.alpha = 0.9

self.tableView.backgroundView = backgroundView self.tableView.backgroundView = backgroundView

You can extend UIView itself and write a class function that would return you this background view correctly configured. 您可以扩展UIView本身并编写一个类函数,该函数将为您返回正确配置的背景视图。 Save this in a file say UIView+extensions.swift 将此保存在UIView + extensions.swift文件中

   extension UIView {

      class func backgroundView()->UIView {

        let backgroundView = UIView()
        backgroundView.translatesAutoresizingMaskIntoConstraints = false
        backgroundView.backgroundColor = UIColor.blackColor()

        let viewsDictionary = ["backgroundView":backgroundView]

        let backgroundView_constraint_H = NSLayoutConstraint.constraintsWithVisualFormat(
                                                                                         "H:[backgroundView(10000)]",
                                                                                         options: NSLayoutFormatOptions(rawValue: 0),
                                                                                         metrics: nil, views: viewsDictionary)
        let backgroundView_constraint_V = NSLayoutConstraint.constraintsWithVisualFormat(
                                                                                         "V:[backgroundView(10000)]",
                                                                                         options: NSLayoutFormatOptions(rawValue:0),
                                                                                         metrics: nil, views: viewsDictionary)

        backgroundView.addConstraints(backgroundView_constraint_H)
        backgroundView.addConstraints(backgroundView_constraint_V)

        backgroundView.alpha = 0.9

        return backgroundView

      }

    }

You can now access this as: 您现在可以通过以下方式访问它:

self.tableView.backgroundView = UIView.backgroundView()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM