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

前端之家收集整理的这篇文章主要介绍了Swift-常用控件创建(UIAlertController,UITableView)等等前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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.
    }
}
待续..
原文链接:https://www.f2er.com/swift/322259.html

猜你在找的Swift相关文章