UIView (一級(jí)標(biāo)題)
UIWindow (子標(biāo)題)
應(yīng)用程序的窗口 (* 或者 - 是無序號(hào)列表)
類似皮影戲的屏幕晌涕, 我們寫的UI控件類似于皮影戲的小人
程序啟動(dòng)
默認(rèn)的是在Xcode中創(chuàng)建項(xiàng)目工程中的 ViewController。如果我們想讓程序從自己建的MyViewController開始執(zhí)行盒粮,就得在 AppDelegate文件中的fun application中添加自己創(chuàng)建的MyViewController
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//2.初始化window UIScreen:
let frame = UIScreen.main.bounds
self.window = UIWindow(frame: frame)
//1.初始化控制器
let myVC = MyViewController()
//設(shè)置成window的根視圖控制器
self.window?.rootViewController = myVC
//3.把window設(shè)置成系統(tǒng)的主window
self.window?.makeKeyAndVisible()
return true
}
在我們創(chuàng)建的MyViewController添加視圖
class MyViewController: 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)
//初始化一個(gè)view
let subView : UIView = UIView(frame: rect)
//添加到父視圖上
subView.backgroundColor = UIColor.red
self.view.addSubview(subView)
//frame:是一種屬性奠滑,代表著坐標(biāo)丹皱,長(zhǎng)寬(大小)
let subView1 = UIView()
subView1.frame = CGRect(x: 140, y: 140, width: 100, height: 100)
subView1.backgroundColor = UIColor.yellow
self.view.addSubview(subView1)
let subView2 = UIView(frame : CGRect(x: 10, y: 10, width: 40, height: 40) )
subView2.backgroundColor = UIColor.blue
/*系統(tǒng)在執(zhí)行時(shí)顯示在subView1上宋税,subView2又在添加在subView 摊崭,最終在顯示在subView*/
subView1.addSubview(subView2)
subView.addSubview(subView2)//顯示在subView
//frame 相對(duì)父視圖的
//bounds 相對(duì)于自身的坐標(biāo)
print(subView2.bounds)
// center
let subview3 = UIView()
self.view.addSubview(subview3)
subview3.frame = CGRect (origin: self.view.center, size: CGSize(width: 100, height: 100))
subview3.backgroundColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
//透明度
//subview3.alpha = 0.1 //這就影響子視圖
//2.透明度(不影響子視圖,只對(duì)subview3其作用杰赛,一般只用它)
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))
subview3.addSubview(subview4)
subview4.backgroundColor = #colorLiteral(red: 0.3411764801, green: 0.6235294342, blue: 0.1686274558, alpha: 1)
//subview4.isHidden = true //影藏
//tag 使用2000以上的(一般用10000以上的)
subview4.tag = 10001
let TagView = subview3.viewWithTag(10001)
print("subview4 = \(subview4), TagView = \(TagView)")
//用戶交互 isUserInteractionEnabled (false:關(guān)閉的 true:打開的)
// self.view.isUserInteractionEnabled = false
// superView :父視圖
print("superView = (subview4.superview ),subview3 = (subview3)")
//子試圖 subviews
/* for item in self.view.subviews {
//從父視圖上移除
item.removeFromSuperview()
}*/
}
//touchesBegan 擊當(dāng)
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("點(diǎn)擊當(dāng)前控制器")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//設(shè)置坐標(biāo) 大小
let rect = CGRect(x: 30, y: 30, width: 100, height: 200)
//初始化一個(gè)view
let subView : UIView = UIView(frame: rect)
//添加到父視圖上
subView.backgroundColor = UIColor.red
self.view.addSubview(subView)
參考: 楊少鋒