使用网页控件(UIWebView)与工具栏控件(UIToolbar),我们可以自制一个小型的浏览器,其功能如下:
源代码下载:使用UIWebView制作一个浏览器.zip
1,输入网址,点击“Go”按钮加载网页
2,加载过程中有进度条,同时可以点击停止按钮取消加载
3,有页面刷新按钮
4,有前进后退按钮
效果图如下:
代码如下:
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
@H_404_148@
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
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_640_404@
184
185
186
187
188
189
190
|
import
UIKit
class
ViewController
:
UIViewController
,
UIWebViewDelegate
UITextFieldDelegate
{
@IBOutlet
var
btngo:
UIButton
!
webview:
UIWebView
!
txturl:
UITextField
!
//进度条计时器
ptimer:
NSTimer
!
//进度条控件
@H_948_502@progBar:
UIProgressView
!
override
func
viewDidLoad() {
super
.viewDidLoad()
// Do any additional setup after loading the view,typically from a nib.
self
.webview.delegate =
;
.view.addSubview(loadindicator);
txturl.delegate =
self
//构建浏览器工具条
setupbrowserToolbar()
}
setupbrowserToolbar()
{
// 创建一个浏览器工具条,并设置它的大小和位置
// 将工具条添加到当前应用的界面中
.view.addSubview(browserToolbar)
path =
NSBundle
.mainBundle().pathForResource(
"back"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,ofType:
"png"
)
urlStr =
NSURL
.fileURLWithPath(path!);
data =
NSData
(contentsOfURL:urlStr);
btnback =
UIBarButtonItem
(image:
UIImage
(data: data!),
style:.
Plain
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,target:
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,action:
Selector
(
"backClicked:"
));
//第一个分隔按钮
btngap1 =
UIBarButtonItem
(barButtonSystemItem:.
FlexibleSpace
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
target:
nil
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
action:
);
// 创建前进按钮 UIBarButtonItem
btnforward =
(named:
"forward.png"
),
"forwardClicked:"
));
// 第二个分隔按钮,创建一个可伸缩的UIBarButtonItem
btngap2 =
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none rgb(249,
);
// 创建重新加载按钮 UIBarButtonItem
btnreload =
"reload.png"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"reloadClicked:"
));
//第三个分隔按钮
btngap3 =
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none rgb(249,
);
//创建加载停止按钮
btnstop =
"stop"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"stopClicked:"
));
//第四个分隔按钮
btngap4 =
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none rgb(249,
@H_502_799@);
//创建进度工具条
progBar =
UIProgressView
(progressViewStyle:
UIProgressViewStyle
Bar
)
// 设置UIProgressView的大小
progBar.frame =
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,80,20)
// 设置该进度条的初始进度为0
progBar.progress = 0
// 创建使用 UIView 的自定义的 UIBarButtonItem
btnprog =
(customView:progBar)
// 为工具条设置工具按钮
browserToolbar.setItems( [btnback,btngap1,btnforward,btngap2,
btnreload,btngap3,btnstop,btngap4,btnprog ],animated:
true
)
//创建计时器对象
ptimer =
NSTimer
.scheduledTimerWithTimeInterval(0.2,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none rgb(249,selector:
"loadProgress"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
userInfo:
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,repeats:
);
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:
UIWebView
)
{
progBar.setProgress(0,153)!important; background:none!important">false
);
ptimer.fire();
loadindicator.startAnimating();
}
webViewDidFinishLoad(webView:
)
{
loadindicator.stopAnimating();
progBar.setProgress(1,monospace!important; min-height:auto!important; background:none!important">);
ptimer.invalidate();
}
loadProgress()
{
// 如果进度满了,停止计时器
if
(progBar.progress >= 1.0)
{
// 停用计时器
ptimer.invalidate();
}
else
{
// 改变进度条的进度值
progBar.setProgress(progBar.progress + 0.02,monospace!important; min-height:auto!important; background:none!important">);
}
}
webView(webView:
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,didFailLoadWithError error:
NSError
?)
{
alertController =
UIAlertController
(title:
"出错!"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
message: error!.localizedDescription,
preferredStyle:
UIAlertControllerStyle
Alert
)
okAction =
UIAlertAction
"确定"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,style:
UIAlertActionStyle
Cancel
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
handler:
@H_502[email protected](okAction)
)
.presentViewController(alertController,completion:
)
}
}
|
源代码下载:使用UIWebView制作一个浏览器.zip
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。