設定画面を開く

Swift3.0
Swift 2.3
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myButton = UIButton(frame: CGRectMake(0,0,100,100))
myButton.backgroundColor = UIColor.redColor()
myButton.layer.masksToBounds = true
myButton.setTitle("設定画面", forState: .Normal)
myButton.layer.cornerRadius = 50.0
myButton.layer.position = CGPoint(x: self.view.frame.width/2, y: self.view.frame.height/2)
myButton.addTarget(self, action: #selector(ViewController.onClickMyButton(_:)), forControlEvents: .TouchUpInside)
self.view.addSubview(myButton)
}
func onClickMyButton(sender: UIButton){
let url = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.sharedApplication().openURL(url!)
}
}
2.3と3.0の差分
- UIColorの参照方法が変更(UIColor.grayColor()->UIColor.gray)
- CGRect,CGPointの初期化方法の変更(CGRectMake,CGPointMakeの廃止)
Reference
- NSURL Class
- UIApplication Class