1,输入网址,点击“Go”按钮加载网页
2,加载过程中有进度条,同时可以点击停止按钮取消加载
3,有页面刷新按钮
4,有前进后退按钮
效果图如下:
代码如下:
@H_301_34@
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@H_404_274@
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
@H_234_404@
184
185
186
187
188
189
190
import
UIKit
class
ViewController
:
UIViewController
,
UIWebViewDelegate
UITextFieldDelegate
{
@IBOutlet
var
btngo:
UIButton
!
webview:
UIWebView
!
txturl:
UITextField
!
loadIndicator:
UIActivityIndicatorView
!
//进度条计时器
ptimer:
NSTimer
!
//进度条控件
@H_137_502@progBar:
UIProgressView
!
override
func
viewDidLoad() {
super
.viewDidLoad()
// Do any additional setup after loading the view,typically from a nib.
self
.webview.delegate =
;
loadIndicator =
(frame:
CGRectMake
(100.0,100.0,32.0,32.0));
loadIndicator.activityIndicatorViewStyle =
UIActivityIndicatorViewStyle
.
Gray
.view.addSubview(loadIndicator);
txturl.delegate =
self
//构建浏览器工具条
setupBrowserToolbar()
}
setupBrowserToolbar()
{
// 创建一个浏览器工具条,并设置它的大小和位置
let
browserToolbar =
UIToolbar
(0,320,44))
// 将工具条添加到当前应用的界面中
.view.addSubview(browserToolbar)
path =
NSBundle
.mainBundle().pathForResource(
"back"
"png"
)
urlStr =
NSURL
.fileURLWithPath(path!);
data =
NSData
(contentsOfURL:urlStr);
btnback =
UIBarButtonItem
(image:
UIImage
(data: data!),
style:.
Plain
Selector
(
"backClicked:"
));
//第一个分隔按钮
btngap1 =
(barButtonSystemItem:.
FlexibleSpace
target:
nil
action:
);
// 创建前进按钮 UIBarButtonItem
btnforward =
(named:
"forward.png"
),
"forwardClicked:"
));
// 第二个分隔按钮,创建一个可伸缩的UIBarButtonItem
btngap2 =
);
// 创建重新加载按钮 UIBarButtonItem
btnreload =
"reload.png"
"reloadClicked:"
));
//第三个分隔按钮
btngap3 =
);
//创建加载停止按钮
btnstop =
"stop"
"stopClicked:"
));
//第四个分隔按钮
btngap4 =
);
//创建进度工具条
progBar =
(progressViewStyle:
UIProgressViewStyle
Bar
)
// 设置UIProgressView的大小
progBar.frame =
// 设置该进度条的初始进度为0
progBar.progress = 0
// 创建使用 UIView 的自定义的 UIBarButtonItem
btnprog =
(customView:progBar)
// 为工具条设置工具按钮
browserToolbar.setItems( [btnback,btngap1,btnforward,btngap2,
btnreload,btngap3,btnstop,btngap4,btnprog ],animated:
true
)
//创建计时器对象
ptimer =
.scheduledTimerWithTimeInterval(0.2,
"loadProgress"
userInfo:
);
ptimer.invalidate()
}
textFieldShouldReturn(textField:
) ->
Bool
{
txturl.resignFirstResponder()
print
"url Changed!"
)
url = txturl.text;
loadUrl(url!)
return
true
}
/*
在 UIWebView 加载指定 URL
*/
loadUrl(url:
String
)
{
urlobj =
(string:url)
request =
NSURLRequest
(
URL
:urlobj!)
webview.loadRequest(request);
}
stopClicked(sender:
)
{
webview.stopLoading()
}
reloadClicked(sender:
)
{
webview.reload()
}
backClicked(sender:
)
{
webview.goBack()
}
forwardClicked(sender:
)
{
webview.goForward()
}
@IBAction
goClicked(sender:
)
{
//收起输入面板
txturl.resignFirstResponder()
url = txturl.text;
loadUrl(url!)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
webViewDidStartLoad(webView:
)
{
progBar.setProgress(0,153)!important">false
);
ptimer.fire();
loadIndicator.startAnimating();
}
webViewDidFinishLoad(webView:
)
{
loadIndicator.stopAnimating();
progBar.setProgress(1,monospace!important; min-height:inherit!important">);
ptimer.invalidate();
}
loadProgress()
{
// 如果进度满了,停止计时器
if
(progBar.progress >= 1.0)
{
// 停用计时器
ptimer.invalidate();
}
else
{
// 改变进度条的进度值
progBar.setProgress(progBar.progress + 0.02,monospace!important; min-height:inherit!important">);
}
}
webView(webView:
NSError
?)
{
alertController =
UIAlertController
(title:
"出错!"
message: error!.localizedDescription,
preferredStyle:
UIAlertControllerStyle
Alert
)
okAction =
UIAlertAction
"确定"
UIAlertActionStyle
Cancel
handler:
)
alertController.addAction(okAction)
.presentViewController(alertController,completion:
)
}
}