我有一个带有搜索图标的顶部栏导航(图像1),我希望能够单击此图标并弹出一个覆盖搜索屏幕(图像2和3)。我是新手,所以我不太确定如何做到这一点。
到目前为止,下面的代码是我创建搜索图标所需要的。
但是不知道从这里去哪里,我似乎找不到任何可以帮助我解决这个问题的东西。帮助将不胜感激。
非常感谢
https://i.stack.imgur.com/OXeT2.png
https://i.stack.imgur.com/FskZS.png
https://i.stack.imgur.com/m3Xto.png
private func setupRightNavItems() {
let searchButton = UIButton(type: .system)
searchButton.setImage(#imageLiteral(resourceName: "search_icon").withRenderingMode(.alwaysOriginal), for: .normal)
searchButton.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: searchButton)
}
答案 0 :(得分:0)
看看UISearchController,您可以使用searchResultsController来初始化它,这是第二个屏幕。然后,当用户点击搜索按钮时,将显示UISearchController。
答案 1 :(得分:0)
2。或者,您可以在image1 viewController上创建一个UIView,然后在该视图中放置一个搜索栏和表格视图。只需在按下搜索按钮时显示并隐藏此UIView。
答案 2 :(得分:0)
嗨,我设法创建了一个深色覆盖层。当我单击搜索图标时,却不知道如何向该覆盖层添加表格视图和搜索栏。谁能帮忙?。
class SearchLauncher: NSObject {
let blackView = UIView()
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}()
@objc func showSearch() {
if let window = UIApplication.shared.keyWindow {
blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)
blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))
window.addSubview(blackView)
window.addSubview(collectionView)
let height: CGFloat = 200
let y = window.frame.height - height
collectionView.frame = CGRect(x: 0, y: window.frame.height, width: window.frame.width, height: height)
blackView.frame = window.frame
blackView.alpha = 0
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
self.blackView.alpha = 1
self.collectionView.frame = CGRect(x: 0, y: y, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
}, completion: nil)
}
}
@objc func handleDismiss() {
UIView.animate(withDuration: 0.5) {
self.blackView.alpha = 0
if let window = UIApplication.shared.keyWindow {
self.collectionView.frame = CGRect(x: 0, y: window.frame.height, width: self.collectionView.frame.width, height: self.collectionView.frame.height)
}
}
}
override init() {
super.init()
//start doing something here maybe
}
}