您的位置:首页 > 移动开发 > Swift

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

2014-12-29 15:29 134 查看
   与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)
    }

效果图如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息