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

**URL:** <https://forums.kodeco.com/t/how-to-programatically-set-a-slider-that-controls-a-image-views-width/88399>\
**Category:** Swift\
**Created:** [November 17, 2019, 5:38pm UTC](https://forums.kodeco.com/t/how-to-programatically-set-a-slider-that-controls-a-image-views-width/88399 "2019-11-17T17:38:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [November 17, 2019, 5:38pm UTC](https://forums.kodeco.com/t/how-to-programatically-set-a-slider-that-controls-a-image-views-width/88399/1 "2019-11-17T17:38:46Z")

</div>

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)

  }}

```

---

<div class="post-metadata">

**Author:** ![jayantvarma](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jayantvarma/32/111768_2.png) [@jayantvarma](https://forums.kodeco.com/u/jayantvarma)\
**Post date:** [December 16, 2019, 10:24am UTC](https://forums.kodeco.com/t/how-to-programatically-set-a-slider-that-controls-a-image-views-width/88399/2 "2019-12-16T10:24:30Z")

</div>

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

cheers,

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [May 2, 2020, 9:38am UTC](https://forums.kodeco.com/t/how-to-programatically-set-a-slider-that-controls-a-image-views-width/88399/3 "2020-05-02T09:38:47Z")

</div>

Closed on May 2, 2020, 9:38am UTC

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