iOS: Context Menu Customization

CHEN SU
CS Random Thoughts on Tech
3 min readMar 31, 2018

--

In iOS, there’re 2 places user could input texts: TextField or WKWebView.

When users are editing, there are context menus that you might want to customize.

What do default context menus look like in TextField and WKWebView?

Sometimes, you don’t like those “Look Up” or “Share…” or “BIU” buttons shown in your app, to avoid unnecessary use cases that might cause issues, and you could inherit from UITextField/WKWebView and make use of the canPerformAction callback to control whether those buttons show or hide.

Instead of hiding the buttons you don’t want to show, what if you want to add some buttons you totally customized?

The way you do it is to modify UIMenuController.shared.menuItems

For detailed codes, please check out my source codes on GitHub.

The key take aways are canPerformAction and UIMenuController.shared.menuItems.

--

--