Skip to content

Instantly share code, notes, and snippets.

@jsbain
Created August 21, 2018 15:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbain/3033d2f71d6913b5b82e6de818c5764e to your computer and use it in GitHub Desktop.
Save jsbain/3033d2f71d6913b5b82e6de818c5764e to your computer and use it in GitHub Desktop.
navview_utils.py
import ui,console,time
from objc_util import on_main_thread
@on_main_thread
def get_top_view(navigation_view):
'''returns the top (aka visible) view from a navigation view'''
import ctypes
nc= navigation_view.objc_instance.navigationController()
tc=nc.topViewController()
v=tc.view()
return v.pyObject(restype=ctypes.py_object,argtypes=[])
@on_main_thread
def get_bottom_view(navigation_view):
'''returns the top (aka visible) view from a navigation view'''
import ctypes
return navigation_view.objc_instance.navigationController().bottomViewController().view().pyObject(restype=ctypes.py_object,argtypes=[])
@on_main_thread
def get_view_stack(navigation_view):
import ctypes
nc= navigation_view.objc_instance.navigationController()
view_list=[]
for vc in nc.viewControllers():
view_list.append(vc.view().pyObject(restype=ctypes.py_object,argtypes=[]))
return view_list
@on_main_thread
def set_view_stack(navigation_view, view_list,animated=True):
'''sets view order. views must have been previously pushed.
e.g to make the top view the new root view:
set_view_stack(nv, [get_view_stack(nv)[-1]])
'''
view_controllers=[]
if not hasattr(view_list,'__iter__'):
view_list=[view_list]
nc= navigation_view.objc_instance.navigationController()
nc_views=get_view_stack(navigation_view)
for i,v in enumerate(view_list):
try:
idx=nc_views.index(v)
view_controllers.append(nc.viewControllers()[idx])
except ValueError:
raise ValueError('view at index {} not on stack ',i)
nc.setViewControllers_animated_(view_controllers,animated)
def pop_to_root(navigation_view,animated=True):
'''pops all views to return to the original root view'''
navigation_view.objc_instance.navigationController().popToRootViewControllerAnimated_(animated)
if __name__=='__main__':
class verboseview(ui.View):
def update(self):
try:
self.counter+=1
except AttributeError:
self.counter=0
console.hud_alert('{0.name} {0.counter}'.format(self))
def layout(self):
console.hud_alert('{0.name} '.format(self))
v=verboseview(name='Hello',frame=(0,0,222,222))
#v.update_interval=0.5
vw=verboseview(name='World',frame=(0,0,222,222))
#vw.update_interval=0.3
nv=ui.NavigationView(v)
nv.present('sheet')
nv.push_view(vw)
print(get_top_view(nv).name)
'''
import ui
from objc_util import *
from objc_hacks import swizzle
def willDismiss(_self,_sel):
pass
swizzle.swizzle(ObjCClass('SUINavigationView_PY3'), 'willDismiss', willDismiss, type_encoding='v@:')
v=ui.View()
nv=ui.NavigationView(v)
nv.present('panel')
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment