Resize UISwitch in Swift 4

那年仲夏 提交于 2020-03-19 05:15:08

问题


I would like to change the default size of the UISwitch in Swift 4.

I have looked at various options but they all relate to v3 and do not work.

Please can someone suggest an example that does so programmatically in Swift 4?

Thank you,

Edit:

I have tried the following examples:

 switchTest.transform = CGAffineTransformMakeScale(0.75, 0.75)

 switchTest.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)

 UISwitch *switchTest = [UISwitch new];
 switchTest.transform = CGAffineTransformMakeScale(0.75, 0.75);

The error message that I received is always the same and says this:

Expected declaration


回答1:


Swift 4 Code

Method 1

Drag UISwitch Storyboard. Make an outlet of your UISwitch and replace ViewDidLoad method by this code.

@IBOutlet weak var switchDemo: UISwitch!

override func viewDidLoad() {
    super.viewDidLoad()
   switchDemo.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
}

Method 2

Make UISwitch through programmatically.

class ViewController: UIViewController {

    let switchSwift4 = UISwitch(frame:CGRect(x: 150, y: 300, width: 0, height: 0))

    override func viewDidLoad() {
        super.viewDidLoad()

        self.switchSwift4.isOn = true
        switchSwift4.setOn(true, animated: false)
        switchSwift4.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
        switchSwift4.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
        self.view!.addSubview(switchSwift4)
    }

    @objc func switchValueDidChange(_ sender: UISwitch) {
        if switchSwift4.isOn == true {
            print("On")
        }
        else {
            print("Off")
        }
    }
}


来源:https://stackoverflow.com/questions/48040403/resize-uiswitch-in-swift-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!