加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 常用控件的创建

发布时间:2020-12-14 07:07:32 所属栏目:百科 来源:网络整理
导读:1、UILabel let label:UILabel = UILabel .init (frame: CGRectMake( 50 , 50 , 100 , 30 ))label .text = "Test" label .textColor = UIColor .redColor ()label .font = UIFont .systemFontOfSize ( 20.0 )label .backgroundColor = UIColor .orangeColor

1、UILabel

let label:UILabel = UILabel.init(frame: CGRectMake(50,50,100,30))
label.text = "Test"
label.textColor = UIColor.redColor()
label.font = UIFont.systemFontOfSize(20.0)
label.backgroundColor = UIColor.orangeColor()
label.textAlignment = NSTextAlignment.Center;
self.view .addSubview(label)

2、UIButton

func createButton() {
     let button = UIButton(type: UIButtonType.System)
     button.frame = CGRectMake(50,50)
     button.setTitle("确定",forState: UIControlState.Normal)
     button.setTitleColor(UIColor.whiteColor(),forState: UIControlState.Normal)
     button.backgroundColor = UIColor.orangeColor()
     button.titleLabel?.font = UIFont.systemFontOfSize(20)
     button.addTarget(self,action: "btnClick:",forControlEvents: UIControlEvents.TouchUpInside)
     button.layer.cornerRadius = 5.0
     self.view.addSubview(button)
    }

func btnClick(button:UIButton) {
     button.backgroundColor = UIColor.redColor()
    }

3、UITextField

let account:UITextField = UITextField.init(frame: CGRectMake(50,200,30))
account.placeholder = "请输入账号"
account.textColor = UIColor.orangeColor()
account.font = UIFont.systemFontOfSize(20)
account.borderStyle = UITextBorderStyle.RoundedRect
self.view .addSubview(account)

4、UIImageView

let imageView:UIImageView = UIImageView(image:UIImage(named:"123"))
imageView.frame = CGRectMake(50,100)
imageView.backgroundColor = UIColor.orangeColor()
self.view .addSubview(imageView)

5、UITableView

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var datas = ["1","2","3","4"]

    override func viewDidLoad() {
        super.viewDidLoad()

        let tableView:UITableView = UITableView(frame: self.view.bounds,style: UITableViewStyle.Plain)
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)

    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }

    func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identifier = "CELL"
        let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier: identifier)

        cell.textLabel?.text = datas[indexPath.row]
        cell.detailTextLabel?.text = "Test"

        return cell
    }

    func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) {
        NSLog("我被点击了%ld",indexPath.row)
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读