2、swift学习-创建基本的控件

来源:互联网 发布:解释型编程语言 编辑:程序博客网 时间:2024/05/21 17:49

   与OC想比较而已,其实用swift创建一些基本控件的方法都是一样的,一些基本的属性都是大同小异,只是语法稍稍不同而已,对于刚刚由OC开始学习swift的可能有点不太习惯,但是没关系,多写多练自然而然就会顺手的。


一、用swift创建一个UILabel

func createLabel() {

        let label = UILabel (frame: CGRectMake(10, 20, self.view.frame.size.width-20, 25))

        label.text = "创建一个UILable"

        label.numberOfLines = 0

       label.textColor = (UIColor.blackColor())

      label.backgroundColor = (UIColor.redColor())

    self.view .addSubview(label)

    }


二、用swift创建一个UIButton

func createButton() {

        let button = UIButton (frame: CGRectMake(10, 50, self.view.frame.width-20, 35))

        (button .setTitle("我是一个按钮", forState: UIControlState.Normal))

        button.backgroundColor = (UIColor.redColor())

        self.view .addSubview(button)

}


三、用swift创建一个UITextField

  func createTextField() {

        let text =  UITextField (frame: CGRectMake(10, 90, self.view.frame.size.width-20, 30))

        text.placeholder = "123"

        text.borderStyle = UITextBorderStyle.RoundedRect

        self.view .addSubview(text)

    }


效果图如下:



0 0
原创粉丝点击