美文网首页
UIWindow与UIView学习

UIWindow与UIView学习

作者: Grt婷 | 来源:发表于2016-11-27 18:58 被阅读0次

1.UIWindow(在AppDelegate.swift中设置其属性)

  • command+c创建MyViewController.swift文件。自定义ViewController

    class MyViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置它的背景颜色
        self.view.backgroundColor=#colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)
      
    }
    
    
  • 将MyViewController的对象设置为window的根视图控制器,把window设置成系统的window

     import UIKit
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions      launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
         //初始化window
        self.window = UIWindow(frame: UIScreen.main.bounds)
        //初始化控制器
        let myVC = MyViewController()
        //设置成window的根视图控制器
        self.window?.rootViewController=myVC
        //把window设置成系统的window
        self.window?.makeKeyAndVisible()
        return true
    }
    

2.UIView


    //将view的视图大小存放在rect中
     let rect = CGRect(x: 30, y: 30, width: 100, height: 200)
     //初始化view并设置其大小
     let subView : UIView = UIView(frame: rect)
     //获取当前控制器的view,设置背景颜色为红色
     subView.backgroundColor=UIColor.red
     //添加到父视图
     self.view.addSubview(subView)
    
     let subView1 = UIView()
     subView1.frame = CGRect(x: 140, y: 240, width: 100, height: 100)
     //设置subView1的背景颜色
     subView1.backgroundColor=UIColor.yellow
     self.view.addSubview(subView1)
     
     let subView2 = UIView()
     subView2.frame = CGRect(x: 10, y: 10, width: 50, height: 50)
     subView2.backgroundColor=UIColor.green
     //subView2添加到subView1上
     subView1.addSubview(subView2)
     //将subView2添加到subView上
     subView.addSubview(subView2)

     //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.8549019694, green: 0.250980407, blue: 0.4784313738, alpha: 1)
     //透明度
     //subView3.alpha = 0.1 //这个属性会影响到放在它上的视图的属性
     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.5843137503, green: 0.8235294223, blue: 0.4196078479, alpha: 1)
     subView3.addSubview(subView4)
     //subView4.isHidden=true//隐藏
     //tag 使用2000以上
     subView4.tag = 10001
     let tagView = subView3.viewWithTag(10001)
     print("subView4 = \(subView4),TagView = \(tagView)")
     //用户交互
     //self.view.isUserInteractionEnabled = false
     //superView
     print("superView = \(subView4.superview),subView3 = \(subView3)")
     //子视图
     for item in self.view.subviews {
         //从父视图上移除
        item.removeFromSuperview()
     }
     
 }
override func touchesBegan(_ touches:Set<UITouch>, with event :UIEvent?){
      print("点击了当前控制器")
 }

  ```

相关文章

网友评论

      本文标题:UIWindow与UIView学习

      本文链接:https://www.haomeiwen.com/subject/aasnpttx.html