2014-06-16 9 views
6

Próbuję wyświetlić obraz na przycisku, Dostaję Blue Print Image nad przyciskiem, próbowałem tak daleko this-Swift - jak wyświetlić obraz nad przyciskiem?

override func viewDidLoad() 
{ 
    super.viewDidLoad() 

    var birdTexture1 = UIImage(named: "fish.png") as UIImage 
    button1.frame = CGRectMake(100, 10, 50, 150) 
    button1.setImage(birdTexture1, forState: .Normal) 

    button1.setTitle("Testing", forState: UIControlState.Normal) 
    button1.addTarget(self, action: "btnAction:", forControlEvents: UIControlEvents.TouchUpInside) 
    self.view.addSubview(button1) 

    // Do any additional setup after loading the view, typically from a nib. 
} 

Dzięki powyższym kodem, ja otrzymuję, następujące dane wyjściowe

enter image description here

Ale rzeczywisty obraz jest to-

enter image description here

+2

zmienić Buttona typ na 'UIButtonTypeCustom'. –

+0

http://stackoverflow.com/questions/21325368/uibutton-background-color-becomes-white-when-running-on-ios6/21325546#21325546 –

+0

@ 0x7fffffff dzięki, to działa ... – ChenSmile

Odpowiedz

3

myślę zastąpić ten kod:

let birdTexture1 = UIImage(named: "fish.png")! as UIImage 
let button1:UIButton=UIButton(frame:CGRectMake(100, 50, 50, 150)) 
button1.setBackgroundImage(birdTexture1, forState: .Normal) 
button1.setTitle("Testing", forState: .Normal) 
button1.addTarget(self, action: #selector(ViewController.btnActionClick(_:)), forControlEvents: UIControlEvents.TouchUpInside) 
self.view.addSubview(button1) 

@IBAction func btnActionClick(sender:AnyObject){ 

} 

i również dodać do obrazu w celu

Powiązane problemy