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

Posted 长沙火山

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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])
    

以上是关于Swift 常用控件的创建 2022年11月更新的主要内容,如果未能解决你的问题,请参考以下文章

Unity版本使用情况统计(更新至2022年10月)

速看!2022年8月编程语言排行榜出炉!第一名太牛啦

Google Play 政策更新 | 2022 年 7 月

Google Play 政策更新 | 2022 年 7 月

2022年11月 .NET CORE工具案例-StackExchange.Redis代码变量方式实现商品秒杀

2022年11月 .NET CORE工具案例-StackExchange.Redis代码变量方式实现商品秒杀