bihmaster bihmaster - 1 month ago 7
iOS Question

Add multiple UIButtons with color images programmatically to the UIViewController scene

I have UIViewController class in which I add multiple color buttons programmatically to the scene. I have 11 different colors, so 11 UIButtons which I add to the scene in viewDidLoad function. See below how I add gray and red color buttons. Then I use function crayonsPressed action to select colors by tag. I do this way because UIButton selection depends on the tag value. As you can see this dependence by UIButton tag value, adding buttons this way in not good approach. I was wondering what would be the best way to programmatically add these 11 color buttons to the scene so that I don't have to repeat below code in viewDidLoad 11 times. My plan is to subclass this UIViewController to 11 more scenes in which they all will have same functionality for all 11 buttons, which is drawing.

Edited to load all image buttons with an array. Still need resolution on how to handle UIButton tag, so that when color button is pressed that color is selected.

class ViewController: UIViewController {

let colors: [(CGFloat, CGFloat, CGFloat)] = [
(0, 0, 0),
(105.0 / 255.0, 105.0 / 255.0, 105.0 / 255.0),
(1.0, 0, 0),
(0, 0, 1.0),
(51.0 / 255.0, 204.0 / 255.0, 1.0),
(102.0 / 255.0, 204.0 / 255.0, 0),
(102.0 / 255.0, 1.0, 0),
(160.0 / 255.0, 82.0 / 255.0, 45.0 / 255.0),
(1.0, 102.0 / 255.0, 0),
(1.0, 1.0, 0),
(1.0, 1.0, 1.0),
]

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let arrayImages : [UIImage] = [UIImage(named: "Grey")!,
UIImage(named: "Red")!,
UIImage(named: "Black")!,
UIImage(named: "Blue")!,
UIImage(named: "Brown")!,
UIImage(named: "DarkGreen")!,
UIImage(named: "DarkOrange")!,
UIImage(named: "Grey")!,
UIImage(named: "LightBlue")!,
UIImage(named: "LightGreen")!,
UIImage(named: "Yellow")!]

var buttonX : CGFloat = 374.5


for images in arrayImages {

let imageButton = UIButton(frame: CGRect(x: buttonX, y: 643, width: 25, height: 125))
buttonX = buttonX + 25
imageButton.setImage(images, for: UIControlState.normal)
imageButton.addTarget(self, action: #selector(crayonsPressed(_:)), for: .touchUpInside)

self.view.addSubview(imageButton)

}

func crayonsPressed(_ sender: AnyObject) {

var index = sender.tag ?? 0
if index < 0 || index >= colors.count {
index = 0
}

(red, green, blue) = colors[index]

if index == colors.count - 1 {
opacity = 1.0
}
}
}

Answer

As rightly suggested by @rmaddy to put the colours in an array and then use those colours in a loop. You may use a variable to keep track of the position of your array.

var i = 0

for images in arrayImages {

    let imageButton = UIButton(frame: CGRect(x: buttonX, y: 643, width: 25, height: 125))
    buttonX = buttonX + 25
    imageButton.tag = i
    imageButton.setImage(images, for: UIControlState.normal)
    imageButton.addTarget(self, action: #selector(self.crayonsPressed(_:)), for: .touchUpInside)

    self.view.addSubview(imageButton)
    i += 1
}

Now each of your buttons will have a valid tag value.

Comments