Мне удалось закодировать четыре элемента UIBarButton, которые появляются при запуске моего приложения.Однако, когда я нажимаю одну из кнопок (чтобы перейти на новый экран), приложение вылетает с ошибкой SIGBART.Из предыдущего опыта я знаю, что это может быть вызвано отсутствием подключения к розеткам.Но поскольку я кодировал кнопки, которые должны отображаться в моем файле, я не уверен, как подключить розетку и действия с помощью кода.Любое руководство будет высоко ценится!
import UIKit
class navigationToolBar: UINavigationController, UIToolbarDelegate {
@IBOutlet weak var searchBarButton1: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
print(UIApplication.shared.statusBarFrame.height)//44 for iPhone x, 20 for other iPhones
navigationController?.navigationBar.barTintColor = .blue
self.toolbar.barTintColor = UIColor.black
var searchicon = UIImage(named: "searchicon")!
var protocolsicon = UIImage(named: "protocolsicon")!
var homeicon = UIImage(named: "homeicon")!
var toolsicon = UIImage(named: "toolsicon")!
let search1 = self.storyboard?.instantiateViewController(withIdentifier: "searchn") as! UINavigationController
let protocols1 = self.storyboard?.instantiateViewController(withIdentifier: "protocolsn") as! UINavigationController
let home1 = self.storyboard?.instantiateViewController(withIdentifier: "homen") as! UINavigationController
// let tools1 = self.storyboard?.instantiateInitialViewController(withIdentifier: "initial") as! imageViewController
// let info1 = self.storyboard?.instantiateViewController(withIdentifier: "Information") as! imageViewController
let toolBar = UIToolbar()
var items = [UIBarButtonItem]()
var searchBarButton1 = UIBarButtonItem(image: searchicon, style: .plain, target: self, action: Selector(("opensearchbar1")))
items.append(
UIBarButtonItem(image: searchicon, style: .plain, target: self, action: Selector(("opensearchbar1")))
)
items.append(
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
)
items.append(
UIBarButtonItem(image: protocolsicon, style: .plain, target: self, action: Selector(("opensearchbar")))
)
items.append(
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
)
items.append(
UIBarButtonItem(image: homeicon, style: .plain, target: self, action: Selector(("onClickBarButton:")))
)
items.append(
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
)
items.append(
UIBarButtonItem(image: toolsicon, style: .plain, target: self, action: Selector(("onClickBarButton:")))
)
items.append(
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
)
items.append(
UIBarButtonItem(image: toolsicon, style: .plain, target: self, action: Selector(("onClickBarButton:")))
)
func opensearchbar(_ sender: UIBarButtonItem) {
switch sender.tag {
case 1:
print("1")
break;
case 2:
print("error")
break;
case 3:
self.navigationController?.pushViewController(protocols1, animated: true)
break;
case 4:
print("error")
break;
case 5:
self.navigationController?.pushViewController(home1, animated: true)
break;
case 6:
print("error")
break;
case 7:
self.navigationController?.pushViewController(home1, animated: true)
break;
case 8:
print("error")
break;
// case 9:
// self.navigationController?.pushViewController(info1, animated: true)
// break;
default:
print("ERROR!!")
}
}
toolBar.setItems(items, animated: true)
toolBar.tintColor = .blue
toolbar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
view.addSubview(toolBar)
toolBar.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
let guide = self.view.safeAreaLayoutGuide
toolBar.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
toolBar.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
toolBar.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
toolBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
else {
NSLayoutConstraint(item: toolBar, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: toolBar, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: toolBar, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true
toolBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
}
func opensearchbar1 (_ sender: UIBarButtonItem) {
let search1 = self.storyboard?.instantiateViewController(withIdentifier: "searchn") as! ViewController
self.navigationController?.pushViewController(search1, animated: true)
}
}