以编程方式创建自定义UIView子类的对象

时间:2018-11-17 16:36:21

标签: ios swift uiview

我编写了一个简单的UIView子类(下面的代码)来显示礼物消息预览,然后将其呈现为PDF。

我能够在Interface Builder中将其设置为UIView的自定义类,但是我也想在代码中创建一个实例。我试图这样做:

let v2 = GiftMessageView(frame: CGRect(x: 0, y: 0, width: A6.height, height: A6.width))
v2.textView.text = (orderDetials["giftMessage"] as! String)

但是,当我加载生成的PDF时,它是空白的,并且我确定这不是PDF渲染的问题,因为从Interface Builder中的实例生成它时可以正常工作。因此,我假设这是子类和/或我用来实例化它的代码的问题。

子类代码(注意-使用SnapKit和SwifterSwift窗格):

import UIKit
import SnapKit
import SwifterSwift

class GiftMessageView: UIView {

    /// A5 Paper Size
    private let A5 = CGSize(width: 420.0, height: 595.0)
    ///A6 Paper Size
    private let A6 = CGSize(width: 298.0, height: 420.0)

    let textView = UILabel()
    let logoView = UIImageView()

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

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

    override func awakeFromNib() {


        logoView.image = UIImage(named: "surprises_dark_pdf")
        logoView.contentMode = .scaleAspectFit

        textView.text = "[ YOUR MESSAGE GOES HERE ]\n\n\n[ LEAVE YOUR NAME OR BE ANONAMOUS ]"
        textView.textAlignment = .center
        textView.numberOfLines = 20
        textView.font = textView.font.withSize(12)


        self.addSubviews([logoView, textView])

        let margin = self.height/20

        logoView.snp.makeConstraints { (make) in
            make.width.equalTo(self.width/2)
            make.height.equalTo(self.height/4)
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(margin)
        }

        textView.snp.makeConstraints { (make) in
            make.top.equalTo(logoView.snp.bottom).offset(margin/10)
            make.left.equalTo(self).offset(margin)
            make.right.equalTo(self).offset(-margin)
            make.bottom.equalTo(self).offset(-margin)
        }

    }

}

1 个答案:

答案 0 :(得分:-1)

您需要将代码移至某个函数

class GiftMessageView: UIView {

    /// A5 Paper Size
    private let A5 = CGSize(width: 420.0, height: 595.0)
    ///A6 Paper Size
    private let A6 = CGSize(width: 298.0, height: 420.0)

    let textView = UILabel()
    let logoView = UIImageView()

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

        shared()
    }

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

        shared()
    }

    override func awakeFromNib() {

        // not called until it's in xib/ib

    }

    func shared(){

        logoView.image = UIImage(named: "surprises_dark_pdf")
        logoView.contentMode = .scaleAspectFit

        textView.text = "[ YOUR MESSAGE GOES HERE ]\n\n\n[ LEAVE YOUR NAME OR BE ANONAMOUS ]"
        textView.textAlignment = .center
        textView.numberOfLines = 20
        textView.font = textView.font.withSize(12)


        self.addSubviews([logoView, textView])

        let margin = self.height/20

        logoView.snp.makeConstraints { (make) in
            make.width.equalTo(self.width/2)
            make.height.equalTo(self.height/4)
            make.centerX.equalTo(self)
            make.top.equalTo(self).offset(margin)
        }

        textView.snp.makeConstraints { (make) in
            make.top.equalTo(logoView.snp.bottom).offset(margin/10)
            make.left.equalTo(self).offset(margin)
            make.right.equalTo(self).offset(-margin)
            make.bottom.equalTo(self).offset(-margin)
        }
    }

}