带有初始化参数的Swift自定义UIView

问题描述 投票:0回答:1

我有一个自定义的UIView类,它带有多个参数,并覆盖了一个空的故事板UIView(子类为MultiPositionTarget类而不是UIview),如下图所示。共9次观看。)>

enter image description here

// Initialize the targets
target1 = MultiPositionTarget(.zero,"targetTemp.png","David","Target")
target1.parentVC = self
targetList.append(target1)

但是,它不接受参数。并且仅加载视图。这是我的下课:

class MultiPositionTarget: UIView{

    var targetName: String!
    var bottomLabelName: String!
    var imageName: String!
    var isSelected: Bool!
    var labelTop: UILabel!
    var labelBottom: UILabel!
    var parentVC: SelectTargetViewController!

    init(_ frame: CGRect,_ imagName: String,_ targetname: String,_ targetlabel: String){

        self.imageName = imagName
        self.targetName = targetname
        self.bottomLabelName = targetlabel
        super.init(frame: frame)
        setUp()
    }

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

    func setUp(){

        let viewWidth = self.bounds.width
        let viewHeight = self.bounds.height
        // Add top label
        labelTop = UILabel(frame: CGRect(x: 0, y: 0, width: viewWidth, height: viewHeight/5))

        //labelTop.center = CGPoint(x: 160, y: 285)
        labelTop.textAlignment = .center
        labelTop.font = UIFont(name:"System",size:6)
        labelTop.text = self.imageName
        labelTop.textColor = UIColor.white
        labelTop.backgroundColor = hexStringToUIColor(hex: "#770B2C")
        //labelTop.alpha = 0.5
        self.addSubview(labelTop)

        let image = UIImage(named: "targetTemp.png")
        let imageView = UIImageView(image: image!)
        imageView.frame = CGRect(x: 0, y: (viewHeight-viewHeight * 4/5), width: viewWidth, height: (viewHeight * 4/5))
        self.addSubview(imageView)

        // Add bottom Label
        labelBottom = UILabel(frame: CGRect(x: 0, y: (viewHeight * 4/5), width: viewWidth, height: viewHeight/5))

        //labelBottom.center = CGPoint(x: 160, y: 285)

        labelBottom.textAlignment = .center
        labelBottom.text = self.bottomLabelName
        labelBottom.font = UIFont(name:"System",size:10)
        labelBottom.textColor = UIColor.white
        labelBottom.backgroundColor = hexStringToUIColor(hex: "770B2C")
        labelBottom.alpha = 0.95
        self.addSubview(labelBottom)

        self.isUserInteractionEnabled = true
        let viewTap = UITapGestureRecognizer(target: self, action: #selector(singleTap))
        self.addGestureRecognizer(viewTap)

    }

}

感谢您的帮助或提示。预先感谢堆栈溢出系列。

<<

此行正在创建一个

视图:
target1 = MultiPositionTarget(.zero,"targetTemp.png","David","Target")
新视图与屏幕上显示的视图无关。实际上,新视图根本不在屏幕上,因为您尚未将其添加到视图层次结构中。即使您这样做,它也是不可见的,因为它的帧为零。

简而言之,您不能调用自定义初始化程序并且也

在情节提要中设计视图。您也可以在情节提要中或在代码中初始化所需的属性。
如果还要初始化情节提要中的属性,则可以使用imageName修改bottomLabelNametargetName@IBInsepctable

@IBInsepctable var targetName: String! @IBInsepctable var bottomLabelName: String! @IBInsepctable var imageName: String! // or, if you want to directly select an image from the storyboard: // @IBInsepctable var image: UIImage!

这样,这些属性将显示在情节提要中的属性检查器中。

如果要在代码中进行初始化,则必须使用赋值语句而不是初始化程序进行设置:

target1.targetName = "David" target1.bottomLabelName = "Target" target1.imageName = "targetTemp.png" target1.parentVC = self targetList.append(target1)

ios swift xcode oop uiview
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.