0

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
        }

       
        
    }
}
  • 1
    The code adds conflicting height constraints each time the gesture recognizer handler is called. You could keep an array of the initial height constraints when the buttons are first created and adjust the `constant` for the existing constraints when the pan occurs. – Geoff Hackworth Apr 10 '23 at 17:53
  • I can’t understand it,can you please explain more or show little bit of code,please,I’m new in programming, the value I get each time I pinch,how to have array of them.. – user8058965 Apr 10 '23 at 18:22
  • 1
    @user8058965 - head over to Google (or your favorite search engine) and search for `swift resize view with pinch gesture` – DonMag Apr 10 '23 at 19:57
  • I can change a view with pinch gesture,but this view got buttons on it,the height of buttons make the view height, – user8058965 Apr 11 '23 at 02:02
  • Geoff Hackworth I did what you , described , it works , know the only problem ,when the view get bigger than 5000 ,the grid view that is on the button view disappears ,basically the grid is not on the screen, when I check the grid is on screen the height is correct but no grid showing, but the buttons work fine, and if its smaller both works ,I gave the answer to the question ,you can see my code, please – user8058965 Apr 11 '23 at 21:23
  • This is the first time you have mentioned a “grid’. We have no idea what your app looks like. Your code is not adding a sub view to the buttons so what does “the grid view that is on the button” mean? You should probably post another question and include some images. – Geoff Hackworth Apr 12 '23 at 08:34
  • This is the new question,it includes the whole code,you can run it,please help if you can , https://stackoverflow.com/questions/75998662/view-disappears-after-changing-the-height-with-pinch-gesture-layer-issue – user8058965 Apr 12 '23 at 23:57

0 Answers0