Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift - 使用UIWebView和UIToolbar制作一个浏览器大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

使用网页控件(UIWebView)与工具栏控件(UIToolbar),我们可以自制一个小型的浏览器,其功能如下: 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
使用网页控件(UIWebView)与工具栏控件(UIToolbar),我们可以自制一个小型的浏览器,其功能如下:
1,输入网址,点击“Go”按钮加载网页
2,加载过程中有进度条,同时可以点击停止按钮取消加载
3,有页面刷新按钮
4,有前进后退按钮

效果图如下:


代码如下:
@H_616_48@
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
@H_616_126@ 38
39
40
@H_696_132@ 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
@H_801_250@ 100
101
102
103
104
105
@H_754_262@ 106
107
108
109
110
111
112
@H_874_276@ 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_873_404@ 184
185
186
187
188
189
190
@H_772_419@
import UIKit
class ViewController : UIViewController , UIWebViewDelegate UITextFieldDelegate {
@IBOutlet var btngo: UIButton !
webview: UIWebView !
txturl: UITextField !
loadInDicator: UIActivityInDicatorView !
//进度条计时器
ptimer: NSTimer !
//进度条控件
@H_192_502@progBar: UIProgressview !
override func viewDidLoad() {
super .viewDidLoad()
// Do any additional setup after loading the view,typically from a nib.
self .webview.delegate = ;
loadInDicator = UIActivityInDicatorView (frame: CGRectMake (100.0,100.0,32.0,32.0));
loadInDicator.activityInDicatorViewStyle = UIActivityInDicatorViewStyle . Gray
.view.addSubview(loadInDicator);
txturl.delegate = self
//构建浏览器工具条
setupBrowserToolbar()
}
setupBrowserToolbar()
{
// 创建一个浏览器工具条,并设置它的大小和位置
@H_801_581@let browserToolbar = UIToolbar (frame: CGRectMake (0,320,44))
// 将工具条添加到当前应用的界面中
.view.addSubview(browserToolbar)
//创建图片工具条,但是不是直接使用文件名,而是用 NSData 方式初始化 UIImage
path = NSBundle .mainBundle().pathForresource( "BACk" :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" )
@H_568_616@
@H_712_618@urlStr = NSURL .fileURLWithPath(path!);
data = NSData (contentsOfURL:urlStr);
btnBACk = UIBarButtonItem (image: UIImage (data: data!),
style:. Plain :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: :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:" ));
@H_419_682@//第一个分隔按钮
btngap1 = UIBarButtonItem (barButtonSy@R_301_2407@Item:. FlexibleSpace :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 :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: );
@H_627_675@// 创建前进按钮 UIBarButtonItem
btnforWARD = (named: "forWARD.png" ),
"forWARDClicked:" ));
// 第二个分隔按钮,创建一个可伸缩的UIBarButtonItem
btngap2 = :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,
675_10793@er56="" index55="" alt1"="" style="outline:0px!important; border:0px!important; bottom: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" :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 = :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,
675_10793@er65="" index64="" alt2"="" style="outline:0px!important; border:0px!important; bottom: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" :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 = :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,
675_10793@er74="" index73="" alt1"="" style="outline:0px!important; border:0px!important; bottom: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,
);
//创建进度工具条
progBar = UIProgressview (progressviewStyle: UIProgressviewStyle Bar )
// 设置UIProgressview的大小
progBar.frame = :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,
675_10793@er95 index94 alt2" style="outline:0px!important; border:0px!important; bottom: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" :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: :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);
@H_616_891@}
stopClicked(sender: )
{
webview.stopLoading()
}
reloadClicked(sender: )
{
webview.reload()
}
BACkClicked(sender: )
{
webview.goBACk()
}
@H_450_929@
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: :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: "出错!" :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,
@H_878_1058@preferredStyle: UIAlertControllerStyle Alert )
okAction = UIAlertAction "确定" :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 :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: )
alertController.addAction(okAction)
.presentViewController(alertController,completion: )
}
}

大佬总结

以上是大佬教程为你收集整理的Swift - 使用UIWebView和UIToolbar制作一个浏览器全部内容,希望文章能够帮你解决Swift - 使用UIWebView和UIToolbar制作一个浏览器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:swiftuitoolbaruiwebview一个使用制作浏览器
猜你在找的Swift相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap