我有一个UIButton。我希望它在屏幕上的随机位置生成。它没有返回错误,并且可以运行,但是屏幕上没有任何显示。我尝试对值进行硬编码,并且行得通。我正在使用Swift。

码:

import UIKit

class ViewController: UIViewController {

    let changingPoint = UIButton()
    let dot = UIImage(named: "sponsor-dot.png")

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        changingPoint.setImage(dot, forState: .Normal)
        changingPoint.addTarget(self, action: "pointPressed:", forControlEvents: .TouchUpInside)
        changingPoint.frame = CGRectMake(CGFloat(arc4random()%(320)), CGFloat(arc4random()%(568)), 150, 150)

        self.view.addSubview(changingPoint)
    }

    func pointPressed(sender: UIButton!) {
        changingPoint.frame = CGRectMake(CGFloat(arc4random()%(320)), CGFloat(arc4random()%(568)), 150, 150)
        println()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

最佳答案

我相信您不会为按钮设置背景色。

changingPoint.backgroundColor = UIColor.blackColor() //or any other color that you want

08-05 14:26