【问题标题】:Swift Custom UIView with init parameters带有初始化参数的 Swift 自定义 UIView
【发布时间】:2020-06-26 04:19:38
【问题描述】:

我已经编写了一个自定义 UIView 类,它接受几个参数并覆盖一个空的故事板 UIView(子类为 MultiPositionTarget 类而不是 UIview),如下所示。下面的视图作为出口链接到视图控制器,从下面的代码中可以看出(共 9 次观看)。

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

    }

}

感谢任何帮助或提示。在此先感谢堆栈溢出系列。

【问题讨论】:

  • 你把// Initialize the targets的代码放在哪里了? target1是插座吗?你为什么要初始化一个插座?
  • 我不认为在通过情节提要添加视图时可以使用自定义初始化程序。您可能需要遍历您拥有的所有插座并以这种方式设置属性。
  • 我想您正在使用插座名称 target1,然后使用您的自定义视图重新分配它。这里你的错误是当你初始化你的插座时,它会从情节提要中中断,因为你的自定义视图是用零帧初始化的,而主视图没有子视图,自定义视图根本不会显示。在这里,您可以创建一个@IBDesignable 类或完全创建一个自定义视图并以编程方式对其进行子视图。

标签: ios swift xcode oop uiview


【解决方案1】:

这一行正在创建一个视图:

target1 = MultiPositionTarget(.zero,"targetTemp.png","David","Target")

新视图与屏幕上显示的视图无关。新视图实际上根本不在屏幕上,因为您还没有将它添加到视图层次结构中。即使你这样做了,它也是不可见的,因为它的框架为零。

简而言之,您不能调用自定义初始化程序在情节提要中设计您的视图。您也可以在情节提要中或在代码中初始化所需的属性。

如果你也想初始化storyboard中的属性,你可以修改imageNamebottomLabelNametargetName@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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多