Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the background of an UIButton programmatically not working

I'm just trying to simply set the background image of the button, but whatever I try it is doing nothing. I also put a UIImageView there to see if there's any issue with the image file, but the image view is set perfectly fine. Do I have to set something in the properties of the button?

Here the code:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let imageToSet = UIImage(named: "3-1")
        
        b1.setBackgroundImage(imageToSet, for: .normal)
        
        imageView.image = imageToSet
    }

    @IBOutlet weak var b1: UIButton!
    @IBOutlet weak var imageView: UIImageView!
}

These are my setting for the button:

Button settings

like image 228
user16071310 Avatar asked Nov 16 '25 03:11

user16071310


1 Answers

In Xcode 13 ,UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on (If you set one of them 4).So when you try to use old behaviours like

setBackgroundImage(..)
setImage(..)

This functions not gonna effect on this button. So If you want to use old behaviours you need to set style Plain to Default

like image 174
Omer Tekbiyik Avatar answered Nov 17 '25 20:11

Omer Tekbiyik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!