【问题标题】:Custom UIButton subclass not displaying title自定义 UIButton 子类不显示标题
【发布时间】:2015-01-07 14:16:17
【问题描述】:

首先让我说我知道这是一个经常被问到的问题,但我似乎找不到与我有相同情况/问题的人。

我正在编写一个音乐应用程序,我想出了一个我喜欢的 UI。它需要一个具有特殊功能的按钮(超越了自定义按钮类型可以实现的功能),所以我决定创建一个UIButton 子类。我在我的子类中使用了以下代码:

required init(coder aDecoder: NSCoder) {
    self.activeState = false
    self.activeAccidental = UIImageView()
    super.init(coder: aDecoder)
    self.layer.borderColor = UIColor(white:221/255, alpha: 1.0).CGColor
    self.layer.borderWidth = 2.0
    self.backgroundColor = UIColor.blackColor()
    self.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    self.setTitle("Hello", forState: .Normal)
}

override func layoutSubviews() {
    println(self.frame.origin.x)
    self.activeAccidental = UIImageView(frame: CGRectMake(self.bounds.origin.x, self.bounds.origin.y, 20, 20))
    self.activeAccidental.image = UIImage(named: "BMICalcIcon.png")
    self.addSubview(activeAccidental)
}

但是,当我向故事板添加按钮(并在字段中输入自定义类名称)时,无论是在初始化程序中设置,还是在故事板的属性检查器中设置,我的标题都是不可见的。这是我在 swift 中的第一个主要项目,所以我不完全确定问题出在哪里。

ImageView 移动到 initWithCoder 时的代码

required init(coder aDecoder: NSCoder) {
    self.activeState = false
    self.activeAccidental = UIImageView()
    super.init(coder: aDecoder)
    self.activeAccidental = UIImageView(frame: CGRectMake(self.bounds.origin.x, self.bounds.origin.y, 20, 20))
    self.activeAccidental.image = UIImage(named: "BMICalcIcon.png")
    self.layer.borderColor = UIColor(white:221/255, alpha: 1.0).CGColor
    self.layer.borderWidth = 2.0
    self.backgroundColor = UIColor.blackColor()
    self.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    self.setTitle("Hello", forState: .Normal)
    self.addSubview(activeAccidental)
}

【问题讨论】:

  • 图层的背景颜色和标题都是白色的。会不会是这个问题?
  • 对不起,我一定是复制代码错了,我现在就修。

标签: ios swift uibutton subclass


【解决方案1】:

也许加[super layoutSubviews]; 我有同样的问题,因为我忘了添加这条线。之后你可以添加你想要的代码。

【讨论】:

  • 很好的答案,让我抓狂了半个小时。
【解决方案2】:

我不知道为什么,但是问题在于将添加图像视图的代码放在 layoutSubviews 中——反正这不是一个好地方,因为它可以被多次调用,这会导致你添加多个图像视图.如果您将该代码移到您的 initWithCoder 方法中,它将正常工作。这是我做的测试类,

import UIKit

class RDButton: UIButton {

    var activeState: Bool
    var activeAccidental: UIImageView

    required init(coder aDecoder: NSCoder) {
        self.activeState = false
        self.activeAccidental = UIImageView()
        super.init(coder: aDecoder)
        self.activeAccidental = UIImageView(frame: CGRectMake(self.bounds.origin.x, self.bounds.origin.y, 20, 20))
        self.activeAccidental.image = UIImage(named: "img.jpg")
        self.layer.borderColor = UIColor(white:221/255, alpha: 1.0).CGColor
        self.layer.borderWidth = 2.0
        self.backgroundColor = UIColor.blackColor()
        self.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        self.setTitle("Hello", forState: .Normal)
        self.addSubview(activeAccidental)

    }
}

【讨论】:

  • 我应该在 layoutSubviews() 方法中留下任何代码吗?
  • 我刚刚尝试将代码移动到 initWithCoder() 方法中。没有骰子。
  • @ericmark26,这很奇怪,我复制了你的代码,按照我说的做了,它对我有用。你看到你的形象了吗?
  • 其实,我把代码放在 layoutSubviews() 方法中的全部原因是因为self.bounds.origin.x.y 在init 方法中都返回0,而它们在init 方法中返回实际值layoutSubviews() 方法。
  • @ericmark26,不是 self.bounds.origin (0,0) 吗?通常是这样。
【解决方案3】:

当当前视图控制器(包含自定义按钮)以performSegue(withIdentifier:sender:) 从除主队列之外的队列启动时也会发生这种情况。要解决此问题,只需在主线程中调用该方法。例如:

class MyViewController: ViewController {
    ...
    func doTask() {
        let myTask = MyTask()
        myTask.perform( {
            Dispatch.main.async {
                self.performSegue(withIdentifier: "second", sender: nil)
            }
        })
    }
}

【讨论】:

    猜你喜欢
    • 2020-06-29
    • 2021-09-21
    • 2016-11-21
    • 1970-01-01
    • 2014-09-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    相关资源
    最近更新 更多