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

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

Swift学习小总结:

UILabel

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

UIButton

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

UITextField

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

UIImageView

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

UIAlertController

  1. //创建一个AlertController
  2. let alertController = UIAlertController(title: "Warning",message: "Test...?",preferredStyle: .alert)
  3. let cancelAction = UIAlertAction(title: "Cancel",style: .cancel,handler: nil);
  4.  
  5. let okAction = UIAlertAction(title: "OK",style: .default,handler: {
  6. action in
  7. print("click the ok!")
  8. })
  9.  
  10. alertController.addAction(cancelAction)
  11. alertController.addAction(okAction)
  12. self.present(alertController,animated: true,completion: nil)

UITableView

  1. import UIKit
  2.  
  3. class TirdViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
  4. var datas = ["1","2","3","4"]
  5.  
  6. override func viewDidLoad() {
  7. super.viewDidLoad()
  8.  
  9.  
  10. let tableView:UITableView = UITableView.init(frame: self.view.bounds,style: UITableViewStyle.plain)
  11. tableView.delegate = self
  12. tableView.dataSource = self
  13.  
  14. }
  15.  
  16. func numberOfSections(in tableView: UITableView) -> Int {
  17. return 1
  18. }
  19.  
  20. func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
  21. return datas.count
  22. }
  23.  
  24. func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  25. let identifier = "CELL"
  26. let cell = UITableViewCell(style: UITableViewCellStyle.subtitle,reuseIdentifier: identifier)
  27. cell.textLabel?.text = datas[indexPath.row]
  28. cell.detailTextLabel?.text = "Test"
  29.  
  30. return cell
  31. }
  32.  
  33. func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
  34. NSLog("Click TableViewCell..",indexPath.row)
  35. }
  36.  
  37. override func didReceiveMemoryWarning() {
  38. super.didReceiveMemoryWarning()
  39. // Dispose of any resources that can be recreated.
  40. }
  41. }
  1. 待续..

猜你在找的Swift相关文章