简体   繁体   中英

Swift menu not showing on right navigation button click

New to Swift. I have a simple UI: put a UINavigationBar on top of a UIWebView , and a the right bar button item to have an action that shows a menu, to allow the use choose different pages to show in the web view.

Show the view controller looks like:

class ViewController: UIViewController {
    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var menu: UIBarMenuItem!
    @IBOutlet weak var viewnav: UIView!

    override func viewDidLoad() {
        super.viewDidLoad();
        let url = URL(string:"about:blank")
        let req = URLRequest(url:url!)
        webView.loadRequest(req)
    }

    @obj func dummy(){
    }

    @IBAction func MenuShow(sender: UIBarButtonItem){
        let menu = UIMenuController.shared
        viewnav.becomeFirstResponder()
        menu.setTargetRect(viewnav.frame, in:viewnav)

        let dummy = UIMenuItem(title:"Dummy", action: #selector(dummy))

        menu.menuItems = [dummy]
        menu.setMenuVisible(true, animated: true)

        //for test only; should move to menu item actions
        let url = URL(string:"https://www.apple.com")
        let req = URLRequest(url:url!)
        webView.loadRequest(req)    
    }

}

(I have connected the web view, the bar button to the UI object; for viewnav I tried adding a new dummy view in Main.storyboard or using the existing navigation bar, both have the same result)

The resulting app shows the empty page and when I hit the menu button, jumps into Apple's home page, so the above code runs as expected. But the menu didn't show up, so what is wrong for the code above?

(there are a few other simular questions like this , but they didn't seem to solving the problem)

This answer gives the solution:

override var canBecomeFirstResponder: Bool {
    return true
}

And add this line to the viewDidLoad method

view.becomeFirstResponder()

Full version:

class ViewController: UIViewController {
    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var menuButton: UIBarMenuItem!

    override func viewDidLoad() {
        super.viewDidLoad();
        let url = URL(string:"about:blank")
        let req = URLRequest(url:url!)
        webView.loadRequest(req)

        view.becomeFirstResponder()

        let menu = UIMenuController.shared
        let dummy = UIMenuItem(title:"Dummy", action: #selector(dummy))
        menu.menuItems = [dummy]
    }

    override var canBecomeFirstResponder: Bool {
        return true
    }

    @obj func dummy(){
        let url = URL(string:"https://www.apple.com")
        let req = URLRequest(url:url!)
        webView.loadRequest(req)    
        menu.setMenuVisible(true, animated: false)
    }

    @IBAction func MenuShow(sender: UIBarButtonItem){
        let menu = UIMenuController.shared
        let bv = menuButton.value(forKey: "view") as? UIView
        menu.setTargetRect(bv!.frame, in:view)

        menu.setMenuVisible(true, animated: true)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM