Swift 常用控件的创建 2022年11月更新

Swift 常用控件的创建

一、基础控件

1.1 UILabel
let label:UILabel = UILabel.init(frame: CGRect(x: 100, y: 100, width: 100, height: 20))
label.text = "测试字符串"
label.textColor = UIColor.red
label.font = UIFont.systemFont(ofSize: 16)
label.backgroundColor = UIColor.orange
label.textAlignment = NSTextAlignment.center
self.view.addSubview(label)
1.2 UIButton
func button() {
        let button:UIButton = UIButton.init(frame: CGRect(x: 100, y: 100, width: 100, height: 40))
        button.setTitle("按钮", for: UIControl.State.normal)
        button.setTitleColor(UIColor.red, for: UIControl.State.normal)
        button.backgroundColor = UIColor.orange
        button.titleLabel?.font = UIFont.systemFont(ofSize: 18)
        button.addTarget(self, action: #selector(buttonClick(button:)), for: .touchUpInside)
        button.layer.cornerRadius = 5
        self.view.addSubview(button)
    }
    
@objc func buttonClick(button:UIButton) {
      button.setTitleColor(UIColor.yellow, for: UIControl.State.normal)
}
1.3 UITextField
  let textField:UITextField = UITextField.init(frame: CGRect(x: 100, y: 100, width: 100, height: 40))
  textField.placeholder = "输入字符串"
  textField.textColor = UIColor.white
  textField.font = UIFont.systemFont(ofSize: 16)
  textField.backgroundColor = UIColor.red
  self.view.addSubview(textField)
1.4 UIImageView
let imageView = UIImageView(image: UIImage(named: "icon_xihu.jpg"))
imageView.frame = CGRect(x: 0, y: 100, width: 64, height: 64)
self.view.addSubview(imageView)
1.5 UITableView
class BaseTableViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {
   
    var tableView: UITableView!

    var datas = ["1","2","3","4"]
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        tableView = UITableView(frame: self.view.bounds, style: UITableView.Style.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, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "CELL"
        let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: identifier)
        
        cell.textLabel?.text = datas[indexPath.row]
        cell.detailTextLabel?.text = "Test"
        
        return cell
    }
   
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        NSLog("我被点击了%ld",indexPath.row)
    }

}
}

二、其他控件

2.1 UIPageControl
import UIKit

var arrColor: NSArray!
var testView: UIView!

class BusinessMainController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "业务类"
        
        arrColor = [UIColor.orange,UIColor.yellow,UIColor.gray,UIColor.blue];

        let pageControl = UIPageControl.init(frame: CGRect(x: 50, y: 120, width: self.view.frame.size.width-100, height: 20))
        pageControl.backgroundColor = UIColor.orange
        pageControl.numberOfPages = 4;
        pageControl.currentPage = 0;
        pageControl.currentPageIndicatorTintColor = UIColor.red
        pageControl.addTarget(self, action: #selector(click(sender:)), for: .touchUpInside)
        self.view.addSubview(pageControl)

        testView = UIView.init(frame: CGRect(x: 10, y: 160, width: self.view.frame.size.width-20, height: 100))
        testView.backgroundColor = UIColor.red
        self.view.addSubview(testView)
    }
    
    @objc func click(sender:UIPageControl) {
        testView.backgroundColor = arrColor[sender.currentPage] as? UIColor
    }
}

2.2 UIDatePicker

import UIKit

var label: UILabel!
var datePicker: UIDatePicker!

class BusinessMainController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "业务类"
        
        label = UILabel.init(frame: CGRect(x: 10, y: 200, width: self.view.frame.size.width-20, height: 20))
        label.textAlignment = NSTextAlignment.center
        label.text = "显示时间"
        self.view.addSubview(label)

        datePicker = UIDatePicker.init(frame: CGRect(x: 10, y: 100, width: self.view.frame.width-20, height: 100))
        datePicker.backgroundColor = UIColor.orange
        datePicker.datePickerMode = UIDatePicker.Mode.date //设置显示的样式
        datePicker.date = NSDate() as Date //设置默认时间
        datePicker.addTarget(self, action: #selector(datePickerValueChange), for: UIControl.Event.valueChanged)

        self.view.addSubview(datePicker)
        
    }
    
    @objc func datePickerValueChange() {

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"

        label.text = dateFormatter.string(from: datePicker.date)
    }
   
}

2.3 UIPickerView

import UIKit

class BusinessMainController: BaseTableViewController,UIPickerViewDataSource,UIPickerViewDelegate {
   
    let arrData = ["北京","上海","广州"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "业务类"
        
        let pickerView = UIPickerView.init()
        pickerView.frame = CGRect(x: 0, y: 20, width: self.view.frame.size.width, height: 200)
        pickerView.delegate = self;
        pickerView.dataSource = self;
        self.view .addSubview(pickerView)
        
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return arrData.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return arrData[row]
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

       NSLog("%@", arrData[row])
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长沙火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值