buttonWithType'不可用:使用对象构造'UIButton(类型:)

时间:2017-07-02 04:13:54

标签: ios swift

在下面的代码中,我们得到错误“buttonWithType”不可用:在“帮助按钮”self.hintButton = UIButton.buttonWithType(.custom)下面的代码行中使用对象构造'UIButton(type :)“。有什么建议。感谢。

    class HUDView: UIView {

      var stopwatch: StopwatchView
      var gamePoints: CounterLabelView


    var hintButton: UIButton!

      //this should never be called
      required init(coder aDecoder:NSCoder) {
        fatalError("use init(frame:")
      }


    override init(frame:CGRect) {
        self.stopwatch = StopwatchView(frame:CGRect(x: ScreenWidth/2-150, y: 0, width: 300, height: 100))
        self.stopwatch.setSeconds(0)

    //the dynamic points label
    self.gamePoints = CounterLabelView(font: FontHUD, frame: CGRect(x: ScreenWidth-200, y: 30, width: 200, height: 70))
    gamePoints.textColor = UIColor(red: 0.38, green: 0.098, blue: 0.035, alpha: 1)
    gamePoints.value = 0

    super.init(frame:frame)

    self.addSubview(gamePoints)

    //"points" label
    let pointsLabel = UILabel(frame: CGRect(x: ScreenWidth-340, y: 30, width: 140, height: 70))
    pointsLabel.backgroundColor = UIColor.clear
    pointsLabel.font = FontHUD
    pointsLabel.text = " Points:"
    self.addSubview(pointsLabel)

    self.addSubview(self.stopwatch)

    self.isUserInteractionEnabled = true

    //load the button image
    let hintButtonImage = UIImage(named: "btn")!

    //the help button
    self.hintButton = UIButton.buttonWithType(.custom)
    hintButton.setTitle("Hint!", for:UIControlState())
    hintButton.titleLabel?.font = FontHUD
    hintButton.setBackgroundImage(hintButtonImage, for: UIControlState())
    hintButton.frame = CGRect(x: 50, y: 30, width: hintButtonImage.size.width, height: hintButtonImage.size.height)
    hintButton.alpha = 0.8
    self.addSubview(hintButton)
  }

  override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    //1 let touches through and only catch the ones on buttons
    let hitView = super.hitTest(point, with: event)

    //2
    if hitView is UIButton {
      return hitView
    }

    //3
    return nil
  }

}

2 个答案:

答案 0 :(得分:1)

只需转换此

self.hintButton = UIButton.buttonWithType(.custom)

到这个

self.hintButton = UIButton(type: .custom)

答案 1 :(得分:0)

将您的按钮初始化写为编译器建议

  

使用对象构造'UIButton(类型:)

self.hintButton = UIButton(type: .custom)