2016-06-07 94 views
-1

我從我的代碼創建一個按鈕。快速按鈕顯示

如何編寫代碼讓按鈕更改爲默認? 我是否缺少一些代碼? 我試圖adjustsImageWhenHighlighted什麼的....

我的意思是,當我按鈕..the按鈕將成爲透明的(我還是推),當我推下會變得回...

喜歡樓下兩個圖像...

import UIKit 

class ViewController: UIViewController { 

var testButton:UIButton = UIButton() 


override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    self.view.backgroundColor = UIColor.whiteColor() 
    testButton.frame = CGRect(x: 100, y: 100, width: 100, height: 100) 
    testButton.backgroundColor = UIColor.whiteColor() 
    testButton.setTitle("123", forState:.Normal) 
    testButton.setTitleColor(UIColor.blueColor(), forState: .Normal) 
    testButton.tag = 1 
    testButton.addTarget(self, action: #selector(number), forControlEvents: .TouchUpInside) 
    self.view.addSubview(testButton) 


} 

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

func number(){ 
    print("\("100")") 
} 

} 

回答

0

你既可以添加:

testButton.setTitleColor(UIColor.redColor(), forState: .Highlighted) 

,或者在按鈕的功能應用的顏色:

@IBAction func testButton(sender: UIButton) { 
    testButton.titleLabel.textColor = UIColor.redColor() 
} 

我用redColor這裏的例子,但你可以改變任何你想要的顏色創造理想的效果。我沒有真正編譯過這個,所以如果你需要糾正任何語法來使它工作,我很抱歉。