I have a view that contains buttons, that I created in viewdidload ,now I need to change the height constraint of the buttons ,to make my view bigger, because the height constraint will change when I pinch, so I need to change the size of the buttons,and the size of the view, depend on the height of the buttons, this is how I did it but it dosent work right ,or is there any way to do it better , after I pinch the note height in my code changes, all over my code the change will take place , but not hier,i call layoutneed it layoutsubview but nothings make it that the button lookout the new note height and set new layout ,
please if you now help me
this the buttons that I created in view controller, viewdidload ,
var previousButton: UIButton!
for i in 0..<rightPianoRollView.numN {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
rightButtonsView.addSubview(button)
button.leadingAnchor.constraint(equalTo: rightButtonsView.leadingAnchor).isActive = true
button.trailingAnchor.constraint(equalTo: rightButtonsView.trailingAnchor).isActive = true
button.heightAnchor.constraint(equalToConstant: noteHeight).isActive = true
if previousButton == nil {
// constrain FIRST button to Top of buttonView
button.topAnchor.constraint(equalTo: rightButtonsView.topAnchor).isActive = true
} else {
// constrain other buttons to Bottom of Previous Button
button.topAnchor.constraint(equalTo: previousButton.bottomAnchor).isActive = true
}
// update previousButton to the current button
previousButton = button
button.setTitle("Button \(i)", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = .red
button.layer.borderWidth = 1.0
button.layer.borderColor = UIColor.green.cgColor
}
// constrain bottom of LAST button to bottom of buttonsView
previousButton.bottomAnchor.constraint(equalTo: rightButtonsView.bottomAnchor).isActive = true
in pinch handle I do this,
@objc func handlePinch(_ g: UIPinchGestureRecognizer) {
guard let constraintright = rightpianorollheightConstraint else { return }
if g.state == .began || g.state == .changed {
print("i pinch")
let newHeightright = constraintright.constant * g.scale
constraintright.constant = max(newHeightright, 10.0) // Ensure the new height is not negative
g.scale = 1.0
targetHeight = newHeightright
noteHeight = calculateNoteHeight(targetHeight: newHeightright, numN: rightPianoRollView.numN)
// Update the height anchor of all buttons
for subview in rightButtonsView.subviews {
guard let button = subview as? UIButton else { continue }
button.heightAnchor.constraint(equalToConstant: noteHeight).isActive = true
}
}
}