cvp
Mar 06, 2019 - 13:23
If you want a special key "Next TextField" above standard keyboard of all TextFields of a view
You have to design your own button (size, border, title, image, ...).
Add one call to your code and it is done
import ui
from objc_util import *
def NextFieldKeyInTextFieldKeyboard(view, button, background_color='lightgray'):
# parameters: view = main view of textfields
# button = ui.Button designed by user (size, title, image...)
# background_color = background_color of InputAccessoryView
# create ui.View for InputAccessoryView above keyboard
v = ui.View() # view above keyboard
v.background_color = background_color # background_color of user choice
v.width = ui.get_screen_size()[0] # width of screen, thus of keyboard
v.height = button.height + 4 # only for Next button
# code executed when Next key is tapped
def Next_tapped(sender):
s = sender.objc_instance.firstResponder()._nextKeyResponder().becomeFirstResponder()
button.action = Next_tapped # code to be executed if button tapped
v.add_subview(button) # set as subview of view
vo = ObjCInstance(v) # get ObjectiveC object of v
# attach to each textfield an InputAccessoryView abobe the keyboard
for sv in view.subviews: # loop on all subviews of main view
if 'TextField' in str(type(sv)): # subview is a TextField
tfo = ObjCInstance(sv).textField()# get ObjectiveC object of sv
tfo.setInputAccessoryView_(vo) # attach our accessory to textfield
if __name__ == '__main__':
main_view = ui.View()
main_view.name = 'NextFieldKeyInTextFieldKeyboard'
main_view.frame = (0,0,280,140)
main_view.add_subview(ui.TextField(frame=(10, 2,260,32)))
main_view.add_subview(ui.TextField(frame=(10, 36,260,32)))
main_view.add_subview(ui.TextField(frame=(10, 70,260,32)))
main_view.add_subview(ui.TextField(frame=(10,104,260,32)))
button = ui.Button() # Button for Next key
button.frame = (0,0,32,32)
button.font = ('<System-Bold>',24)
button.title = '⏯' # emoji as title
NextFieldKeyInTextFieldKeyboard(main_view,button)#,background_color='green')
main_view.present('sheet')
