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

Swift-常用控件创建(UIAlertController,UITableView)等等

发布时间:2020-12-14 06:29:00 所属栏目:百科 来源:网络整理
导读:Swift学习小总结: UILabel //自定义一个Label let label:UILabel = UILabel .init (frame: CGRect( x : 20 , y : 20 ,width: 100 ,height: 30 )) label .text = "TestLabel" label .textColor = UIColor .red label .font = UIFont .systemFont (ofSize: 20

Swift学习小总结:

UILabel

//自定义一个Label
        let label:UILabel = UILabel.init(frame: CGRect(x: 20,y: 20,width: 100,height: 30))
        label.text = "TestLabel"
        label.textColor = UIColor.red
        label.font = UIFont.systemFont(ofSize: 20.0)
        label.backgroundColor = UIColor.orange
        label.textAlignment = NSTextAlignment.center
        self.view .addSubview(label)

UIButton

//自定义一个Button
        let button = UIButton(type: UIButtonType.system)
        button.frame = CGRect(x: 20,y: 60,width: 80,height: 45)
        button.setTitle("OK",for: UIControlState.normal)
        button.setTitleColor(UIColor.white,for: UIControlState.normal)
        button.backgroundColor = UIColor.orange
        button.titleLabel?.font = UIFont.systemFont(ofSize: 20.0)
        button.addTarget(self,action: Selector(("btnClick:")),for: UIControlEvents.touchUpInside)
        button.layer.cornerRadius = 5.0
        self.view.addSubview(button)

UITextField

//创建UITextField
        let nameTextField:UITextField = UITextField.init(frame: CGRect(x: 20,y: 120,height: 30))
        nameTextField.placeholder = "Input your name"
        nameTextField.textColor = UIColor.orange
        nameTextField.font = UIFont.systemFont(ofSize: 20)
        nameTextField.borderStyle = UITextBorderStyle.roundedRect
        self.view.addSubview(nameTextField)

UIImageView

//创建UIImageView
        let imageView:UIImageView = UIImageView(image:UIImage(named:"test"))
        imageView.frame = CGRect(x: 20,y: 150,height: 100)
        imageView.backgroundColor = UIColor.blue
        self.view.addSubview(imageView)

UIAlertController

//创建一个AlertController
        let alertController = UIAlertController(title: "Warning",message: "Test...?",preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "Cancel",style: .cancel,handler: nil);

        let okAction = UIAlertAction(title: "OK",style: .default,handler: {
            action in
            print("click the ok!")
         })

        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController,animated: true,completion: nil)

UITableView

import UIKit

class TirdViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    var datas = ["1","2","3","4"]

    override func viewDidLoad() {
        super.viewDidLoad()


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

    }

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

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

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> 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,didSelectRowAt indexPath: IndexPath) {
        NSLog("Click TableViewCell..",indexPath.row)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
待续..

(编辑:李大同)

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

    推荐文章
      热点阅读