首先介绍一下文件的结构
class ViewController: UIViewController {
//声明成员变量
override func viewDidLoad() {
super.viewDidLoad()
//代码实现
// Do any additional setup after loading the view, typically from a nib.
}
//代码调用的函数
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
注意:Swift代码中会出现"!"和"?",在此不述,你可以去看看大神们写的
//UILabel
let label = UILabel(frame: CGRect(x: 50, y: 50, width: 200, height: 50))
label.text = "你好,斯威夫特"
label.textColor = UIColor.blue
label.textAlignment = .center
label.backgroundColor = UIColor.red
label.font = UIFont.systemFont(ofSize: 30)
self.view.addSubview(label)
//UIButton的使用
let btn = UIButton(type: UIButtonType.system)
btn.frame = CGRect(x:50, y: 100, width: 100, height: 50)
btn.backgroundColor = UIColor.gray
btn.setTitle("按钮", for: UIControlState.normal)
btn.titleLabel?.font = UIFont.systemFont(ofSize: 30)
btn.setTitleColor(UIColor.red, for: UIControlState.normal)
btn.layer.cornerRadius = 15
btn.addTarget(self, action:#selector(btnAction1(sender:)), for: .touchUpInside)
self.view?.addSubview(btn)
//button Action
func btnAction1(sender:UIButton){
print("点击了")
//UIAlertController的使用
let alert = UIAlertController(title: "警告", message:nil , preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "确定", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (UIAlertAction) in
print("取消了")
}))
self.present(alert, animated: true, completion: nil)
}
//UIImageView
let imageV = UIImageView(frame: CGRect(x: 0, y: KScreenHeight-KScreenWidth, width: KScreenWidth, height: KScreenWidth))
imageV.image = UIImage.init(named: "123.jpeg")
self.view.addSubview(imageV)
//UITextView
let textV = UITextView(frame: CGRect(x:150, y: 100, width: 100, height: 50))
textV.textColor = UIColor.white
textV.backgroundColor = UIColor.black
self.view.addSubview(textV)
//UITextField
let textF = UITextField(frame: CGRect(x:250, y: 50, width: 100, height: 50))
textF.textColor = UIColor.black
textF.placeholder = "输入"
textF.borderStyle = .line
self.view.addSubview(textF)
//UISlider
let slider = UISlider(frame: CGRect(x:100, y: 150, width: 100, height: 50))
self.view.addSubview(slider)
//要开启 NSAllowsArbitraryLoads为yes,不然存在报错可能
//UIWebView
let webView = UIWebView(frame: self.view.bounds)
let url = NSURL(string:"https://www.baidu.com")
let request = NSURLRequest(url: url! as URL)
webView.loadRequest(request as URLRequest)
self.view.addSubview(webView)
//UITableView
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
var tableView : UITableView?
var dataA = ["北京","上海","深圳","广州","天津","南京","杭州","武汉"]
override func viewDidLoad() {
super.viewDidLoad()
self.createTableView()
}
func createTableView(){
self.tableView = UITableView.init(frame:self.view.bounds,style:.plain)
self.tableView?.delegate = self
self.tableView?.dataSource = self
self.view.addSubview(self.tableView!)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataA.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "cell"
let cell = UITableViewCell.init(style:.default, reuseIdentifier: identifier)
cell.textLabel?.text = dataA[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//取消选中状态
tableView.deselectRow(at: indexPath, animated: true)
let str = dataA[indexPath.row]
print("用户选择了\(str)")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//UICollectionView
//可以不签订layout
class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource{
let KScreenWidth = UIScreen.main.bounds.size.width
let KScreenHeight = UIScreen.main.bounds.size.height
var collectionV : UICollectionView?
override func viewDidLoad() {
super.viewDidLoad()
self.createCollection()
}
func createCollection(){
//设置Layout
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical//滚动方向
layout.itemSize = CGSize(width: (KScreenWidth - 30)/2, height: 80)
layout.sectionInset = UIEdgeInsetsMake(24, 0, 0, 0);
//collection
self.collectionV = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
collectionV?.dataSource = self
collectionV?.delegate = self
collectionV?.backgroundColor = UIColor.white
collectionV?.register(UICollectionViewCell().classForCoder, forCellWithReuseIdentifier: "item");
self.view.addSubview(collectionV!)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return 60
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "item", for: indexPath);
cell.backgroundColor = UIColor.red
return cell
}
//UICollectionViewDelegateFlowLayout
// func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{
// return UIEdgeInsetsMake(5, 10, 5, 10)
// }
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}