用swif 語言如何創(chuàng)建UIView

UIView (一級標題)

UIWindow (子標題)

應用程序的窗口 (* 或者 - 是無序號列表)

類似皮影戲的屏幕, 我們寫的UI控件類似于皮影戲的小人

程序啟動

默認的是在Xcode中創(chuà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()
//設置成window的根視圖控制器
self.window?.rootViewController = myVC
//3.把window設置成系統(tǒng)的主window
self.window?.makeKeyAndVisible()

return true

}

在我們創(chuàng)建的MyViewController添加視圖

class MyViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
//獲取當前控制器的view,設置背景顏色為紅色
//self.view.backgroundColor = UIColor.red

//設置坐標 大小
let rect = CGRect(x: 30, y: 30, width: 100, height: 200)
//初始化一個view
let subView : UIView = UIView(frame: rect)
//添加到父視圖上
subView.backgroundColor = UIColor.red
self.view.addSubview(subView)

//frame:是一種屬性,代表著坐標,長寬(大小)
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í)行時顯示在subView1上,subView2又在添加在subView ,最終在顯示在subView*/
subView1.addSubview(subView2)
subView.addSubview(subView2)//顯示在subView
//frame 相對父視圖的

//bounds 相對于自身的坐標
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.透明度(不影響子視圖,只對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:關閉的 true:打開的)

// self.view.isUserInteractionEnabled = false
// superView :父視圖
print("superView = (subview4.superview ),subview3 = (subview3)")

//子試圖 subviews

/* for item in self.view.subviews {
//從父視圖上移除
item.removeFromSuperview()

}*/

}
//touchesBegan 擊當
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("點擊當前控制器")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

//設置坐標 大小
let rect = CGRect(x: 30, y: 30, width: 100, height: 200)
//初始化一個view
let subView : UIView = UIView(frame: rect)
//添加到父視圖上
subView.backgroundColor = UIColor.red
self.view.addSubview(subView)

參考: 楊少鋒

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容