Forum Archive

Popover for nav bar button

robertiii

I found this code somewhere on the internet heehee.

def menuButton(sender):
    pv = ui.View()
    pv.name = 'popover'
    pv.frame = (0,0,200,200)
    x = sender.x + sender.width/2
    y = sender.superview.titlebar_height + sender.y + sender.height/2
    pv.present('popover',popover_location = (x,y))  

However this doesn’t work with navbar buttons as they don’t have the location information. How can I get the location info?

cvp

@robertiii try this

import ui
from objc_util import *

v = ui.View()
v.background_color = 'white'

def b_action(sender):
    vi = ObjCInstance(b).view()
    r = vi.convertRect_toView_(vi.bounds(),None)
    # x,y in screen coordinates, not in view
    x,y,w,h = r.origin.x,r.origin.y,r.size.width,r.size.height
    po = ui.View()
    po.frame = (0,0,200,200)
    po.present('popover',popover_location=(x+w/2,y+h))

b = ui.ButtonItem()
b.title = 'popover'
b.action = b_action
v.right_button_items = (b,)
v.present('fullscreen')

cvp

You can also get position in a view with

import ui
from objc_util import *

v = ui.View()
v.background_color = 'white'

def b_action(sender):
    vi = ObjCInstance(b).view()
    #r = vi.convertRect_toView_(vi.bounds(),None)
    r = vi.convertRect_toView_(vi.bounds(),ObjCInstance(v))
    # x,y in view coordinates
    x,y,w,h = r.origin.x,r.origin.y,r.size.width,r.size.height
    po = ui.View()
    po.frame = (0,0,200,200)
    po.present('popover',popover_location=(x+w/2,y+h))

b = ui.ButtonItem()
b.title = 'popover'
b.action = b_action
v.right_button_items = (b,)

v.frame = (0,0,300,300)
v.present('sheet')
#v.present('fullscreen')

stephen

@cvp AWESOME TY! i was going to need this info for item details in my inventory 🙃