一下代码全部在 didFinishLaunchingWithOptions 初始化事件:
//设置window窗体颜色
self.window = UIWindow(frame: UIScreen.mainScreen().bounds);
self.window?.backgroundColor=UIColor.redColor();
self.window?.makeKeyAndVisible();
// 绑定视图控制器,可以将初始化数据在 制定的ViewControler执行。
self.window?.rootViewController = ViewController()
// 新建视图
var rootView=UIView(frame: CGRect(x: 0, y: 0, 400, height: 400));
rootView.backgroundColor = UIColor.whiteColor();
self.window?.addSubview(rootView);
// 新增lable
var lable1 = UILabel(frame: CGRect(x: 100,y: 200, 100,height: 50))
lable1.text="Hello";
lable1.textColor = UIColor.blackColor();
lable1.tag = 2 ;
rootView.addSubview(lable1);
//新增button
var button1 = UIButton(frame: CGRect(x: 100,y: 100, 100,height: 50));
button1.setTitle("ClickMe", forState: UIControlState.Normal);
button1.backgroundColor = UIColor.blackColor();
// 代码绑定事件
button1.addTarget(self, action: "button1Click:", forControlEvents: UIControlEvents.TouchUpInside);
rootView.addSubview(button1);
// 约束 相对位置 约束条件
//rootView.addConstraint(NSLayoutConstraint(item: <#AnyObject#>, attribute: <#NSLayoutAttribute#>, relatedBy: <#NSLayoutRelation#>, toItem: <#AnyObject?#>, attribute: <#NSLayoutAttribute#>, multiplier: <#CGFloat#>, constant: <#CGFloat#>)
return true