How to programatically set a slider that controls a image views width

My code below is written in all code no storyboard. What I want to do use the slider move to change the width of the imageView pic. What I have tried below does not work. When the slider moves I want the width of pic to move along with it.

                      import UIKit

    class ViewController: UIViewController {

    var pic = UIImageView()
    var move = UISlider()

    var oldCons = [NSLayoutConstraint]()
   var box = 20
  override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
pic.backgroundColor = UIColor.systemYellow

   [pic,move].forEach({
       $0.translatesAutoresizingMaskIntoConstraints = false
       self.view.addSubview($0)
   })

oldCons = [
       pic.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :150),
       pic.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -200),
       pic.widthAnchor.constraint(equalToConstant: CGFloat(box)),
       pic.heightAnchor.constraint(equalToConstant: 450)
    ]
   NSLayoutConstraint.activate(oldCons)




   NSLayoutConstraint.activate ([
       move.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :37.5),
       move.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 225),
       move.widthAnchor.constraint(equalToConstant: 200),
       move.heightAnchor.constraint(equalToConstant: 50),

   ])
   move.addTarget(self, action: #selector(moveRight), for: .touchUpInside)


   }

     @objc func moveRight() {
NSLayoutConstraint.deactivate(oldCons)
NSLayoutConstraint.activate ([
    pic.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :50),
    pic.topAnchor.constraint(equalTo: view.centerYAnchor, constant : -50),
    pic.widthAnchor.constraint(equalToConstant: CGFloat(box)),
    pic.heightAnchor.constraint(equalToConstant: 450)
])

     box = Int(CGFloat(20 + move.value*90))
  print(box)

  }}

Hi @timswift,
I believe i have responded to this on the other thread you created.

cheers,

This topic was automatically closed after 166 days. New replies are no longer allowed.