import UIKit
//继承UIView
class demoView : UIView{
//获取屏幕尺寸
let main = UIScreen.main.bounds.size
//初始化
override init(frame: CGRect) {
super.init(frame: frame)
//设置背景为灰色
self.backgroundColor = UIColor.gray
}
//自动布局
override func layoutSubviews() {
//设置view的大小和位置
self.frame = CGRect(x: 0, y: 0, width: main.width, height: main.height)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
一个简单的灰色背景的View就创建成功了!
然后在你的UIViewController里面加载demoView就可以了代码如下
import UIKit
class beginView: UIViewController {
let demo = demoView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(demo)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
网友评论