I am using the 26 letters of the alphabet and creating a UIButton
with the respective letter, after I create the array of UIButtons
I send the Array in which I store these buttons and I add it to a stack view.
The stackview is also inside of a UIView
i called abcBtnView
.
It works if i pass it the entire UIButton
array, but 26 buttons vertically or horizontally does not look good. So i decided to instead of sending an array of 26 buttons i would send 6 arrays, 5 with 5 UIbuttons
and One array with 1 button.
The error i get is that i can't convert UIButton
into type UIView
.
The parameter UIStackview(arrangedSubviews: [UIView])
is of type [UIView]
however it still took my array of [UIButton]
the first time but it doesn't take it if i put more than one Array of UIbuttons
.
I am wondering how i can add multiple UIButton arrays to UIStackview so that i can have 6 columns and 5 rows of UIButton?
THINNGS I HAVE TRIED:
- Try to typecast [UIButton] to [UIView]
- Created an arrays that holds an array of UIButtons and then USED A for loop to add each UIstackview into our view(abcBtnView).
THE CODE BELOW WORKS, BUT I CAN ONLY PASS IN ONE UIBUTTON.
private func makeABCbtns(){
let abcde = createButtons(named: "A", "B", "C", "D", "E")
let fghij = createButtons(named: "F", "G", "H", "I", "J")
let klmno = createButtons(named: "K","L", "M", "N", "O")
let pqrst = createButtons(named: "P", "Q","R", "S", "T")
let uvwxy = createButtons(named: "U", "V", "W","X", "Y")
let z = createButtons(named: "Z")
let stackView = UIStackView(arrangedSubviews: abcde)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.spacing = 1
stackView.distribution = .fillEqually
// UIView where all the buttons will be in.
abcBtnView.addSubview(stackView)
//I am giving the stackview the size of the abcBtnView.
stackView.anchor(top: abcBtnView.topAnchor,
leading: abcBtnView.leadingAnchor,
bottom: abcBtnView.bottomAnchor,
trailing: abcBtnView.trailingAnchor,
centerXaxis: nil,
centerYaxis: nil)
}
func createButtons(named: String...) -> [UIButton]{
return named.map { letter in
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle(letter, for: .normal)
button.backgroundColor = .green
button.setTitleColor( .blue , for: .normal)
return button
}
}