UIView控件

 1.ViewController.swift

import UIKitclass ViewController: UIViewController {    override func viewDidLoad() {        super.viewDidLoad()       

 //获取当前控制器的view,设置背景颜色为红色     

   //self.view.backgroundColor = UIColor.red      

  //初始化方法,设置大小坐标       

 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相对于父视图  

      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  会影响视图        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以下系统用        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("点击了当前控制器")

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ViewsBecause view objects are the main way your applicati...
    梁光飞阅读 773评论 0 0
  • VLC的集成和使用 VLC介绍 VLC Media Player (VideoLAN) 为 Windows、Lin...
    Pocket阅读 20,384评论 75 66
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,273评论 5 13
  • 在澳大利亚,塔斯马尼亚的Swansea小镇,有关鸡的物件是常见的居家摆设。当地人告诉我,这是繁荣与兴旺的象征。在新...
    雪花雪语阅读 754评论 0 3
  • 醉后才知酒浓,爱过才知情重。 很多时候我并不懂原来爱一个人的爱是什么直到你转身那一刻起风才涌起方悔太不珍惜 也很多...
    扎如阅读 229评论 1 1

友情链接更多精彩内容