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

本文介绍了如何使用Swift实现常见的UI组件,包括UILabel、UIButton、UITextField、UIImageView及UIAlertController的配置方法,并提供了一个UITableView的具体实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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, width: 100, 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, width: 100, 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.
    }
}
                           待续..
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值