Programmatically set image and text on UIButton

后端 未结 6 1753
孤街浪徒
孤街浪徒 2020-12-28 13:46

I need to create button programatically with an image for normal and highlighted state as well text. I cannot build it using Interface Builder, because I need to create butt

6条回答
  •  温柔的废话
    2020-12-28 14:11

    UIButtons setImage sets the image above the title so you will not be able to see the text below it. So try to set image to your button with setBackgroundImage.

    Objective-C:

    [btn setBackgroundImage:buttonImage forState:UIControlStateNormal]; 
    

    Swift:

    myButton.setBackgroundImage(buttonImage, forState: .normal)
    

提交回复
热议问题