Search code examples
iosswiftuislider

Having trouble setting position of UISlider in spritekit for all devices?


Im using this code to set the position correctly for all my devices. The problem Im having is if I set the position of the slider in the 4s the iphone 6+ position changes. Same goes for my other devices. How do I get every device to perfectly position the sliders? Im in spritekit btw.

//iphone4s
if UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.maxLength < 568.0 {
    middleSlider = UISlider(frame: CGRectMake(180, 50, 150, 20))
    middleSlider.tintColor = UIColor.whiteColor()
    middleSlider.setThumbImage(UIImage(named: "thumb2"), forState: UIControlState.Normal)
    self.view?.addSubview(middleSlider)
    bottomAudioControlBg.position = CGPointMake(self.size.width / 2, self.size.height / 7)

}



//iphone5
if UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.maxLength == 568.0 {

}



//iphone6
if UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.maxLength == 667.0 {

}



//iphone6+
if UIDevice.currentDevice().userInterfaceIdiom == .Phone && ScreenSize.maxLength == 736.0 {
    middleSlider = UISlider(frame: CGRectMake(255, 50, 150, 20))
    middleSlider.tintColor = UIColor.whiteColor()
    middleSlider.setThumbImage(UIImage(named: "thumb2"), forState: UIControlState.Normal)
    self.view?.addSubview(middleSlider)


}




//ipad
if UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.maxLength == 1024.0 {

}



//ipadpro
if UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.maxLength == 1366.0 {

}

Solution

  • This is way to much code for such a problem:)

    Using a simple scaling-factor you can easily solve this:

     let scaleFactor = UIScreen.mainScreen().bounds.width / 320
    
            let middleSlider = UISlider(frame: CGRectMake(180 * scaleFactor, 50, 150, 20))
            middleSlider.tintColor = UIColor.whiteColor()
            middleSlider.setThumbImage(UIImage(named: "thumb2"), forState: UIControlState.Normal)
            self.view?.addSubview(middleSlider)