美文网首页
创建简单的UIButton, UILabel, UITextFi

创建简单的UIButton, UILabel, UITextFi

作者: 代码干货 | 来源:发表于2015-06-15 17:31 被阅读57次

    How to create a button programmatically?
    //创建一个button
    <pre>
    let button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
    button.frame = CGRectMake(100, 100, 100, 50)
    button.backgroundColor = UIColor.blueColor()
    button.setTitle("test button", forState: UIControlState.Normal)
    button.addTarget(self, action:"buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button)
    </pre>

    //创建一个label
    <pre>
    let label = UILabel()
    label.frame = CGRectMake(CGRectGetMinX(button.frame), CGRectGetMaxY(button.frame)+20, 200, 21)
    label.backgroundColor = UIColor.redColor()
    label.textAlignment = NSTextAlignment.Center
    label.text = "请叫我老大 哼哼。。"
    self.view.addSubview(label)
    </pre>

    //创建UITextField
    <pre>
    var textField: UITextField = UITextField()
    textField.frame = CGRectMake(CGRectGetMinX(label.frame), CGRectGetMaxY(label.frame) + 20, 100, 50)
    textField.backgroundColor = UIColor.grayColor()
    self.view.addSubview(textField)

    </pre>

    相关文章

      网友评论

          本文标题:创建简单的UIButton, UILabel, UITextFi

          本文链接:https://www.haomeiwen.com/subject/dxqsqttx.html