setting an image inside a button by code

Discussion in 'iOS Programming' started by theprizerevealed, Oct 29, 2016.

  1. theprizerevealed macrumors member

    Joined:
    Feb 26, 2016
    #1
    I am trying to display an image inside a button, I know how to do it by using the Attributes Inspector (it works when I do it that way) but I wish to do it by manual coding also. I know to put my images in Assets.xassets.

    I tried this code below but it simply displayed a colored block inside the button. Can someone help me understand what's wrong? thanks for your help.



    Code:
    let image = UIImage(named: "mypicture")
          
            self.button.setImage(image, for: UIControlState.selected)
    
    
     
  2. Floris macrumors 68020

    Floris

    Joined:
    Sep 7, 2007
    Location:
    Netherlands

Share This Page