繁体   English   中英

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

[英]Create object of custom UIView subclass programatically

我编写了一个简单的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)
        }

    }

}

您需要将代码移至函数

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)
        }
    }

}

暂无
暂无

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

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