gpt4 book ai didi

ios - 单击时在 BarButtonItem 下方显示下拉菜单(弹出窗口)

转载 作者:可可西里 更新时间:2023-11-01 00:19:44 25 4
gpt4 key购买 nike

当我单击“+”时,我想在右侧的 BarButtonItem 下显示菜单(弹出窗口),其中有两个选项。按这些选项之一将导致其他 View Controller 。

我正在使用 Xcode 10 和 Swift 4.2。

enter image description here

像这样: enter image description here

这是我现在的代码,没有任何反应。我做错了什么?我可以用不同的方式写吗?

import Foundation

class RootVC: UITableViewController {

@IBOutlet weak var openSideMenu: UIBarButtonItem!

let itemArray = ["1", "2", "3"]

override func viewDidLoad() {
super.viewDidLoad()

openSideMenu.target = self.revealViewController()
openSideMenu.action = #selector(SWRevealViewController.revealToggle(_:))
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RootCell", for: indexPath)

cell.textLabel?.text = itemArray[indexPath.row]

return cell
}

@IBAction func addBarButtonPressed(_ sender: UIBarButtonItem) {
let menu = UIMenuController.shared
menu.menuItems =
[UIMenuItem(title: "Test me", action: Selector("deleteLine")),
UIMenuItem(title: "Test me", action: Selector("deleteLine")),
UIMenuItem(title: "Test me", action: Selector("deleteLine"))]

menu.setTargetRect((self.navigationItem.rightBarButtonItems?.first?.frame)!, in: self.view)
becomeFirstResponder()
menu.setMenuVisible(true, animated: true)

}

}
extension UIBarButtonItem {

var frame: CGRect? {
guard let view = self.value(forKey: "view") as? UIView else {
return nil
}
return view.frame
}

}

最佳答案

我使用 AssistoLab/DropDown CocoaPods ( link ) 解决了我的问题

enter image description here

enter image description here

这是代码:

import Foundation
import DropDown

class ViewController: UIViewController {

@IBOutlet weak var addBarButton: UIBarButtonItem!

let rightBarDropDown = DropDown()

override func viewDidLoad() {
super.viewDidLoad()

rightBarDropDown.anchorView = addBarButton
rightBarDropDown.dataSource = ["Generate New", "Add Manual"]
rightBarDropDown.cellConfiguration = { (index, item) in return "\(item)" }
}

@IBAction func showBarButtonDropDown(_ sender: AnyObject) {

rightBarDropDown.selectionAction = { (index: Int, item: String) in
print("Selected item: \(item) at index: \(index)") }

rightBarDropDown.width = 140
rightBarDropDown.bottomOffset = CGPoint(x: 0, y:(rightBarDropDown.anchorView?.plainView.bounds.height)!)
rightBarDropDown.show()
}
}

关于ios - 单击时在 BarButtonItem 下方显示下拉菜单(弹出窗口),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52576425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com