美文网首页iOS Developer
Swift3.0几个常用控件

Swift3.0几个常用控件

作者: 千年积木 | 来源:发表于2017-06-14 11:15 被阅读17次

后续会更新。。。

UILabel
UIButton
UIView
UITextField
UITextView
UIAlertController
UITableView
UIPageControl
UIImageView
UIDatePicker
UIWebView
UIScrollView
UISlider
UIActionSheet
UISegmentedControl
UINavigationController
UIPickerView
UIStepper
UIProgressView
UIActivityIndicatorView

部分代码。。。

import UIKit
import WebKit

class ViewController: UIViewController,UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,WKUIDelegate,WKNavigationDelegate{

var scrollView:UIScrollView!
var imageView:UIImageView!
var wkWeb1 = WKWebView()//初始化
var wkWeb2:WKWebView!
var wkWeb3:WKWebView?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    
    //分号可以不加,看个人习惯!!!!
    
    //MARK:UILabel
    let  label = UILabel.init(frame: CGRect.init(x: 20, y: 64, width: 210, height: 30));
    label.text = "hello,World";
    label.backgroundColor = UIColor.red;
    label.layer.borderWidth = 1.0;
    label.layer.borderColor = UIColor.black.cgColor;
    self.view.addSubview(label);
    
    //MARK:UIView
    let view = UIView.init(frame: CGRect.init(x: 20, y: 100, width: 210, height: 30));
    view.backgroundColor = UIColor.orange;
    self.view.addSubview(view);
    
    
    //MARK:UIButton
    let bt1 = UIButton.init(type: UIButtonType.custom);
    bt1.frame = CGRect.init(x: 20, y: 150, width: 210, height: 30)
    bt1.setTitle("bt1快点我", for: UIControlState.normal);
    bt1.setTitleColor(UIColor.white, for: UIControlState.normal);
    bt1.backgroundColor = UIColor.orange;
    bt1.addTarget(self, action: #selector(ViewController.btAction), for: UIControlEvents.touchUpInside);
    bt1.tag = 111;
    self.view.addSubview(bt1);
    
    //MARK:
    let textField = UITextField.init(frame: CGRect.init(x: 20, y: 220, width: 310, height: 30));
    textField.backgroundColor = UIColor.red;
    textField.placeholder = "芝麻开门";
    textField.delegate = self;
    self.view.addSubview(textField);
    
    
    //MARK:UItableView
    let tableView = UITableView.init(frame: CGRect.init(x: 10, y: 260, width: 210, height: 200), style: UITableViewStyle.grouped);
    tableView.delegate = self;
    tableView.rowHeight = 30.0;
    tableView.tableFooterView = UIView.init();
    tableView.dataSource = self;
    
    self.view.addSubview(tableView);
    
    
    //Mark:UIScrollView
    scrollView =  UIScrollView.init(frame: CGRect.init(x: 230, y:260, width: 120, height: 200));
    
    scrollView.contentSize = CGSize.init(width: 240, height: 0);
    scrollView.backgroundColor = UIColor.red;
    scrollView.delegate = self;
    scrollView.isPagingEnabled = true;
    self.view.addSubview(scrollView);
    
    //MARK:UIImageView
    imageView = UIImageView.init(image: UIImage.init(named:"481"), highlightedImage: nil);
    imageView.frame = CGRect.init(x: 10, y: 470, width: 150, height: 150);
    self.view.addSubview(imageView);
    
    
    //MARK:WKWebView
    //1
    wkWeb1.frame = CGRect.init(x: 230, y: 64, width: 50, height: 50);
    wkWeb1.load(URLRequest.init(url: URL.init(string: "https://www.baidu.com")!));
    wkWeb1.navigationDelegate = self;
    wkWeb1.uiDelegate = self;
    self.view.addSubview(wkWeb1);

    //2
    wkWeb2 = WKWebView.init();
    wkWeb2.frame = CGRect.init(x: 170, y: 470, width: 100, height: 100);
    wkWeb2.load(URLRequest.init(url: URL.init(string: "https://www.baidu.com")!));
    self.view.addSubview(wkWeb2);

    //3
    wkWeb3 = WKWebView.init();
    wkWeb3?.frame = CGRect.init(x: 170, y: 590, width: 50, height: 50);
    wkWeb3?.load(URLRequest.init(url: URL.init(string: "https://www.baidu.com")!));
    self.view.addSubview(wkWeb3!);
    
}


//scrollViewDelegate
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
    let x = scrollView.contentOffset.x;
    
    print(x);
    
}


//tableViewDelgate
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell");
    
    if cell == nil{
        
        cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell");
    }
    
    return cell!;
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5;
}


//textFieldDelegate
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder();
    return true;
}


func btAction(bt:UIButton){
    print("hello world!,\(bt.tag)");
    
    //MARK:UIAlertController
    let alertController = UIAlertController.init(title: "温馨提示", message: "你的快递到了!", preferredStyle: UIAlertControllerStyle.alert);
    
    let action = UIAlertAction.init(title: "哈哈", style: UIAlertActionStyle.default) { (UIAlertAction) in
        
        print("消失了");
        
    }
    
    alertController.addAction(action);
    
    self.present(alertController, animated: true) {
        
    };

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    
    print("1122334455");
    
    let vc = ViewController2.init()
    
    
    self.present(vc, animated: true, completion: nil);
    
}

}

class ViewController2:UIViewController{

override func viewDidLoad() {
    super.viewDidLoad()
    
    self.view.backgroundColor = UIColor.red;
}



override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
    self.dismiss(animated: true, completion: nil);
}

}

相关文章

网友评论

    本文标题:Swift3.0几个常用控件

    本文链接:https://www.haomeiwen.com/subject/tossqxtx.html