
環境:Xcode 8.3, Swift 3
import UIKit
class ViewController: UIViewController {
let label1:UILabel = UILabel();
let label2:UILabel = UILabel();
let button1:UIButton = UIButton();
let button2:UIButton = UIButton();
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
label1.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
label1.text = "Label1"
label1.backgroundColor = UIColor.blue self.view.addSubview(label1)
label2.frame = CGRect(x: 50, y: 50, width: 300, height: 300)
label2.text = "Label2"
label2.backgroundColor = UIColor.yellow
self.view.addSubview(label2)
button1.frame = CGRect(x: 0, y: 400, width: 200, height: 50)
button1.backgroundColor = UIColor.blue
button1.setTitle("Send Label1 to Back", for: .normal)
button1.addTarget(self, action: #selector(self.touchUpButton1), for: .touchUpInside)
self.view.addSubview(button1)
button2.frame = CGRect(x: 210, y: 400, width: 200, height: 50)
button2.backgroundColor = UIColor.red
button2.setTitle("Send Label1 to Front", for: .normal)
button2.addTarget(self, action: #selector(self.touchUpButton2), for: .touchUpInside) self.view.addSubview(button2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func touchUpButton1(){
//To Back
self.view.sendSubview(toBack: label1)
}
func touchUpButton2(){
//To Front self.view.bringSubview(toFront: label1)
}
}
コメントをお書きください