用swif 语言如何创建UIView

UIView (一级标题)

UIWindow (子标题)

应用程序的窗口 (* 或者 - 是无序号列表)

类似皮影戏的屏幕, 我们写的UI控件类似于皮影戏的小人

程序启动

默认的是在Xcode中创建项目工程中的 ViewController。如果我们想让程序从自己建的MyViewController开始执行,就得在 AppDelegate文件中的fun application中添加自己创建的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设置成系统的主window
self.window?.makeKeyAndVisible()

return true

}

在我们创建的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
/*系统在执行时显示在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, 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)

参考: 杨少锋

你可能感兴趣的:(用swif 语言如何创建UIView)