我想在我的webview应用程序中添加一个后退按钮。
通过UIBarButtonItem
查看,我似乎无法找到后退按钮。见代码:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL(string: "http://google.com")!
webView.loadRequest(NSURLRequest(URL: url))
webView.allowsBackForwardNavigationGestures = true
let refresh = UIBarButtonItem(barButtonSystemItem: .Refresh, target: webView, action: #selector(webView.reload))
let back = UIBarButtonItem(barButtonSystemItem: .Back, target: webView, action: #selector(webView.goBack))
toolbarItems = [back, refresh]
navigationController?.toolbarHidden = false
}
如何在工具栏中添加后退按钮?
答案 0 :(得分:4)
我认为您忘了在UIBarbuttonItem
navigationItem
因此您需要执行以下代码:
self.navigationItem.rightBarButtonItems = toolbarItems // that show two right side button.
如果你想在navigationbar
显示左侧的后退按钮和右侧的刷新按钮,那么你必须是这样的代码:
self.navigationItem.rightBarButtonItem = refresh
self.navigationItem.leftBarButtonItem = back
这将出现在你的导航后退按钮左侧和刷新按钮右侧
所以你的代码看起来如下:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL(string: "http://google.com")!
webView.loadRequest(NSURLRequest(URL: url))
webView.allowsBackForwardNavigationGestures = true
let refresh = UIBarButtonItem(barButtonSystemItem: .Refresh, target: webView, action: #selector(webView.reload))
//let back = UIBarButtonItem(barButtonSystemItem: .Back, target: webView, action: #selector(webView.goBack))
let back = UIBarButtonItem(title: "Back",style: .Plain,target: webView,action: #selector(webView!.goBack))
self.navigationItem.rightBarButtonItem = refresh
self.navigationItem.leftBarButtonItem = back
navigationController?.toolbarHidden = false
}
如果你想在leftbarbutton添加多个按钮,那么再创建一个UIBarbuttonItem
对象:
let undo = UIBarButtonItem(barButtonSystemItem: .Undo, target: webView, action: #selector(webView.undo))
self.navigationItem.leftBarButtonItems = [back,undo]
将在导航栏上显示两个左按钮