otter
otter

Reputation: 93

Changing UISlider x and y position

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.

Upvotes: 1

Views: 1322

Answers (2)

Reinier Melian
Reinier Melian

Reputation: 20804

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

Upvotes: 0

Ratul Sharker
Ratul Sharker

Reputation: 8011

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.

Upvotes: 2

Related Questions