1

I have viewController with slider inside and I need to change the position of slider programmatically. I am using following code:

mySliderOutlet.frame = CGRectMake(100, 250, mySliderOutlet.frame.size.width, mySliderOutlet.frame.size.height)

Problem is this doesn't do anything. Outlet is connected correctly, I am using it elsewhere in the code to read values from slider. I will be glad for any suggestions. Thanks.

otter
  • 93
  • 2
  • 8

2 Answers2

2

Please make sure, following this are done before setting the frames on your slider

  1. Autolayout is turned off and sizing classes are disabled in the storyboard.Here is how to do it
  2. Turn off the 'auto resize subview' for the view inside the viewcontroller from storybaord.Uncheck the autoresize subview option
  3. Make sure this frame update your'e doing must be done in Main thread or main queue.
Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
  • Yes, this does the trick. But I was hoping there is a way to move slider around and still be able to use constraints. Is there a way to do it? Anyway thanks a lot. I am gonna use this solution. – otter Jul 26 '16 at 21:05
  • 1
    if you want to keep constraint in storyboard and still want to update the frame or bound from the code, you need to take the outlet of the constraintss and update their status from the code. – Ratul Sharker Jul 27 '16 at 01:54
0

With AffineTransform you can rotate your UISlider layer try this code for this results

enter image description here

Swift

class ViewController: UIViewController {

    @IBOutlet weak var verticalSlider: UISlider!
    func DEGTORAD(deg:Double) -> CGFloat
    {
      return CGFloat(deg * M_PI / 180.0)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        verticalSlider.layer.setAffineTransform(CGAffineTransformMakeRotation(DEGTORAD(90)))
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

I hope this helps you

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
  • Thank you, but this isn`t what I need. I know how to rotate slider, my problem is I need to move it. @RatulSharker gave me an answer. – otter Jul 26 '16 at 21:08
  • 1
    @otter, You can change the constraints constant values to move your UISlider to any position you want to – Reinier Melian Jul 26 '16 at 21:15