UIToolbar swift

//
// ViewController.swift
// UILabelTest
//
// Created by mac on 15/6/23.
// Copyright (c) 2015年 fangyuhao. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UIWebViewDelegate,UITextFieldDelegate{
@IBOutlet weak var btngo: UIButton!
@IBOutlet weak var webview: UIWebView!
@IBOutlet weak var txturl: UITextField!
var loadIndicator:UIActivityIndicatorView!

//进度条计时器
var ptimer:NSTimer!
//进度条控件
var progBar:UIProgressView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.webview.delegate = self
    loadIndicator = UIActivityIndicatorView(frame: CGRectMake(100, 100, 32, 32))
    loadIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    self.view.addSubview(loadIndicator)
    txturl.delegate = self
    //构建浏览器工具条
    setupBrowserToolbar()


}
func setupBrowserToolbar(){
    //创建一个浏览器工具条,并设置它的大小和位置
    var browserToolbar = UIToolbar(frame: CGRectMake(0, 20, 320, 44));
    //将工具条添加到当前应用的界面中
    self.view.addSubview(browserToolbar)
    //创建图片工具条,但是不是直接使用文件名,
    var path = NSBundle.mainBundle().pathForResource("back", ofType: "png")
    var urlStr = NSURL.fileURLWithPath(path!)
    var data = NSData(contentsOfURL:urlStr!)
    var btnback = UIBarButtonItem(image: UIImage(data: data!), style: UIBarButtonItemStyle.Bordered, target: self, action: Selector("backClicked:"))
    //第一个分隔按钮
    var btngap1 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target:nil, action: nil)
    //创建前进按钮
    var btnforward = UIBarButtonItem(image: UIImage(named: "forward.png"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("forwardClicked:"))
    //第二个分隔按钮
    var btngap2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    //创建重新加载按钮
    var btnreload = UIBarButtonItem(image: UIImage(named: "reload.png"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("reloadClicked:"))
    var btngap3=UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace,target: nil, action: nil)
    //创建加载停止按钮
    var btnstop = UIBarButtonItem(image: UIImage(named: "stop"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("stopClicked:"))
    //第四个分隔按钮
    var btngap4=UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace,target: nil, action: nil)
    //创建进度工具条
    progBar = UIProgressView(progressViewStyle: UIProgressViewStyle.Bar)

    //设置UIProgressview的大小
    progBar.frame = CGRectMake(0, 0, 80, 20)
    //设置该进度条的初始进度为0
    progBar.progress = 0
    //创建使用UIView的自定义的UIBarButtonItem
    var btnprog = UIBarButtonItem(customView: progBar)
    //为工具条设置工具按钮
    browserToolbar.setItems([btnback,btngap1,btnforward,btngap2,btnreload,btngap3,btnstop,btngap4,btnprog], animated: true)
    //创建计时器对象
    ptimer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector("loadProgress"), userInfo: nil, repeats: true)
    ptimer.invalidate()
}
func stopClicked(sender:UIBarButtonItem){
    webview.stopLoading()
}
func reloadClicked(sender:UIBarButtonItem){
    webview.reload()
}
func backClicked(sender:UIBarButtonItem){
    webview.goBack()
}
func forwardClicked(sender:UIBarButtonItem){
    webview.goForward()
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    txturl.resignFirstResponder()
    println("url Changed!")
    var url = txturl.text
    loadUrl(url)
    return true
}
func loadUrl(url:String){
    var urlobj = NSURL(string: url)
    var request = NSURLRequest(URL: urlobj!)
    webview.loadRequest(request)
}
@IBAction func goClicked(sender: UIButton) {
    txturl.resignFirstResponder()
    var url = txturl.text
    loadUrl(url)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
func webViewDidStartLoad(webView: UIWebView) {
    progBar.setProgress(0, animated: false)
    ptimer.fire()
    loadIndicator.startAnimating()
}
func webViewDidFinishLoad(webView: UIWebView) {
    loadIndicator.stopAnimating()
    progBar.setProgress(1, animated: true)
    ptimer.invalidate()
}
func loadProgress(){
    //如果进度满了,停止计时器
    if(progBar.progress>=1.0){
        //停用计时器
        ptimer.invalidate()
    }else{
        //改变进度条的进度值
        progBar.setProgress(progBar.progress+0.02, animated: true)
    }
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
    var alertview = UIAlertView()
    alertview.title = "出错"
    alertview.message = error.localizedDescription
    alertview.addButtonWithTitle("确定")
    alertview.show()
}

}

posted @ 2016-12-16 23:36  乱笙歌  阅读(175)  评论(0编辑  收藏  举报