UIView子类(Swift4) - 自定义方法(setNumber)无法按预期工作

时间:2018-05-28 20:03:25

标签: swift uiview subclass

我创建了一个名为UIBingoBall的UIView子类来显示一个宾果球(见下文)。为简洁起见,我省略了初始化 - 但课程按原样运作。我可以实例化它,设置3个属性“myimage,myLetterLabel和myNumberLabel”并查看正确的宾果球。

但是 - 我正在尝试向类中添加一个名为.setNumber(ballNumber:Int)的方法,它将适当地改变球的3个属性 - 一步到位 - 但是当我这样做时,它只是部分工作。它正在改变图像并正确显示图像。它正在改变标签文本 - 但它没有显示它。 如果我单独更改值(而不是调用函数),一切正常。

有人可以快速查看并告诉我我做错了什么吗?或解释为什么行为不同?

这是我想要使用的功能:

func setNumber(ballNumber: Int) {if ballNumber > 75 || ballNumber < 1 { return }

    let fontModifier = frame.height / 70
    let ballLetter:Int = (ballNumber - 1) / 15
    self.myLetterLabel.text = bingoLetterArray[ballLetter]
    self.myNumberLabel.text = "\(ballNumber)"
    self.myimage.image = bingoBallImageArray[ballLetter]
    self.myNumberLabel.font = UIFont.systemFont(ofSize: fontModifier / 24, weight: .heavy)
    self.myLetterLabel.font = UIFont.systemFont(ofSize: fontModifier / 8, weight: .heavy)

}

以下是属性定义:

class UIBingoBall: UIView {

private let bingoBallImageArray: [UIImage] = [#imageLiteral(resourceName: "BingoBlankB"),#imageLiteral(resourceName: "BingoBlankI"),#imageLiteral(resourceName: "BingoBlankN"),#imageLiteral(resourceName: "BingoBlankG"),#imageLiteral(resourceName: "BingoBlankO")]
private let bingoLetterArray: [String] = ["B", "I", "N", "G", "O"]


var myimage: UIImageView = {
    let image = UIImageView()
    image.image = #imageLiteral(resourceName: "BlankBallB")
    image.contentMode = .scaleAspectFill
    image.backgroundColor = .clear
    image.translatesAutoresizingMaskIntoConstraints = false
    return image
}()

var myLetterLabel: UILabel = {
    let label = UILabel()
    label.text = "B"
    label.textColor = .blue
    label.backgroundColor = .clear
    label.textAlignment = .center
    label.numberOfLines = 1
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont.systemFont(ofSize: 8, weight: .bold)
    return label
}()

var myNumberLabel: UILabel = {
    let label = UILabel()
    label.text = "1"
    label.textColor = .blue
    label.backgroundColor = .clear
    label.textAlignment = .center
    label.numberOfLines = 1
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont.systemFont(ofSize: 24, weight: .heavy)
    return label
}()

并且,如果它们是必要的,这是我从init调用的setupViews()。

func setupViews() {
    addSubview(myimage)
    addSubview(myLetterLabel)
    addSubview(myNumberLabel)

    myimage.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    myimage.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
    myimage.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
    myimage.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
    myNumberLabel.centerYAnchor.constraint(equalTo: myimage.centerYAnchor, constant: 3).isActive = true
    myNumberLabel.centerXAnchor.constraint(equalTo: myimage.centerXAnchor).isActive = true
    myLetterLabel.topAnchor.constraint(equalTo: myNumberLabel.topAnchor,constant: -8).isActive = true
    myLetterLabel.heightAnchor.constraint(equalToConstant: 10).isActive = true
    myLetterLabel.centerXAnchor.constraint(equalTo: myimage.centerXAnchor).isActive = true

}

提前感谢任何可以(并且确实)尝试提供帮助的人!

1 个答案:

答案 0 :(得分:0)

我认为问题在于字体

let fontModifier = frame.height / 70

self.myNumberLabel.font = UIFont.systemFont(ofSize: fontModifier / 24, weight: .heavy)
self.myLetterLabel.font = UIFont.systemFont(ofSize: fontModifier / 8, weight: .heavy)

它的值可以设置为0,帧高度应该是70 * 24 = 1680的倍数,为标签的字体大小赋予1,这肯定是不可预测的