?1.ViewController.swift
import UIKitclass ViewController: UIViewController {? ? override func viewDidLoad() {? ? ? ? super.viewDidLoad()? ? ? ?
?//獲取當(dāng)前控制器的view,設(shè)置背景顏色為紅色? ? ?
?? //self.view.backgroundColor = UIColor.red? ? ??
? //初始化方法,設(shè)置大小坐標(biāo)? ? ? ?
?let rect = CGRect(x: 30, y: 30, width: 100, height: 200)? ? ? ? let subview:UIView = UIView(frame:rect)? ? ? ??
//把子視圖加載上父視圖??
? ? ? subview.backgroundColor = UIColor.red? ? ? ? self.view.addSubview(subview)? ? ? ??
//frame相對(duì)于父視圖??
? ? ? let subview1 = UIView()? ? ? ? subview1.frame = CGRect(x: 140, y: 240, width: 100, height: 100)? ? ? subview1.backgroundColor = UIColor.yellow? ? ? ? self.view.addSubview(subview1)? ? ? ? ? ? ? ? ? ? ??
? //center? ? ? ?
?let subview3 = UIView()? ? ? ? self.view.addSubview(subview3)? ? ? ? subview3.frame = CGRect(origin: self.view.center, size: CGSize(width: 20, height: 20))? ? ? ? ? ? subview3.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)? ? ? ? ?
?//透明度? ? ? ?
?//subview3.alpha = 0.1? 會(huì)影響視圖? ? ? ? subview3.backgroundColor = UIColor(colorLiteralRed: 0.5, green: 0.5, blue: 0.5, alpha: 0.5)? ? ? ? let subview4 = UIView(frame: CGRect(x: 10, y: 10, width: 40, height: 40))? ? ? ? subview4.backgroundColor = #colorLiteral(red: 0.5725490451, green: 0, blue: 0.2313725501, alpha: 1)? ? ? ? subview3.addSubview(subview4)? ? ? ?
?//tag值使用2000以上,100以下系統(tǒng)用? ? ? ? subview4.tag = 10001? ? ? ? let Tagview = subview3.viewWithTag(10001)? ? ? ? print("subview4 = \(subview4),Tagview = \(Tagview)")? ? ? ? ? ? ? ? //subview4.isHidden = true
?//隱藏? ? ? ? //用戶交互? ??
? ? self.view.isUserInteractionEnabled = true? ? ?
?? ? //superview是父試圖? ? ? print("subview = \(subview4.superview),subview3 = \(subview3)")? ? for item in self.view.subviews{? ? ? ? ? ??
//從父試圖上移除? ?
?? ? ? // item.removeFromSuperview()? ? ? ? ? ? print(item)? ? ? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? override func touchesBegan(_ touches: Set, with event: UIEvent?) {
print("點(diǎn)擊了當(dāng)前控制器")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}