码迷,mamicode.com
首页 > 其他好文 > 详细

代码构建视图

时间:2015-04-24 22:29:34      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

   一下代码全部在 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, width: 400, height: 400));

        rootView.backgroundColor = UIColor.whiteColor();

        self.window?.addSubview(rootView);

            

        // 新增lable

        var lable1 = UILabel(frame: CGRect(x: 100,y: 200,width: 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,width: 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

 

代码构建视图

标签:

原文地址:http://www.cnblogs.com/pengzhaov/p/4454532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!