Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
segmented control
-
Anyone know how to set the background color for a segmented
control to white? I can set it to blue or yellow or most any color except white. Looking on line I gather this was a recent development
with ios. Most of the solutions I’ve found seem like a lot of work for a color. -
@Appletrain Try this to set background of selected element to white, but perhaps do you want something else
Edit: ok, sorry, I understand what you say, my mistake. Pythonista or iOS does not allow to put background as white, perhaps because standard selected is white
Google search shows that this is so since iOS 13.Edit2:
see next post for a solution of your problem
import ui from objc_util import * v = ui.View() v.frame = (0,0,400,200) v.background_color = 'lightgray' d = 64 s = 12 sc = ui.SegmentedControl() items = ['aaa','bbb','ccc','ddd'] sc.segments = items sc.frame = (10,10,d*len(items),d) def sc_action(sender): idx = sender.selected_index o = ObjCInstance(sender).segmentedControl() #print(dir(o)) idx = o.selectedSegmentIndex() for i in range(len(items)): if i == idx: with ui.ImageContext(d,d) as ctx: path = ui.Path.rounded_rect(0,0,d,d,5) ui.set_color('white') path.fill() s = 12 ui.draw_string(items[idx], rect=(0,(d-s)/2,d,s), font=('Menlo', s), color='black', alignment=ui.ALIGN_CENTER) ui_image = ctx.get_image().with_rendering_mode(ui.RENDERING_MODE_ORIGINAL) o.setImage_forSegment_(ui_image.objc_instance,idx) else: o.setTitle_forSegmentAtIndex_(items[i],i) sc.action = sc_action v.add_subview(sc) v.present('sheet')
-
@Appletrain forget previous post if you want, but this works for your problem
import ui from objc_util import * v = ui.View() v.frame = (0,0,400,200) v.background_color = 'lightgray' d = 64 s = 12 def setcol(o,i,color): with ui.ImageContext(d,d) as ctx: path = ui.Path.rounded_rect(0,0,d,d,5) ui.set_color(color) path.fill() s = 12 ui.draw_string(items[i], rect=(0,(d-s)/2,d,s), font=('Menlo', s), color='black', alignment=ui.ALIGN_CENTER) ui_image = ctx.get_image().with_rendering_mode(ui.RENDERING_MODE_ORIGINAL) o.setImage_forSegment_(ui_image.objc_instance,i) sc = ui.SegmentedControl() items = ['aaa','bbb','ccc','ddd'] sc.segments = items sc.frame = (10,10,d*len(items),d) o = ObjCInstance(sc).segmentedControl() for i in range(len(items)): setcol(o,i,'white') def sc_action(sender): idx = sender.selected_index o = ObjCInstance(sender).segmentedControl() for i in range(len(items)): col = 'blue' if i == idx else 'white' setcol(o,i,col) sc.action = sc_action v.add_subview(sc) v.present('sheet')
-
Thank you @cvp , I’ll give this a try. I saw several solutions similar to this but, I had no idea how to change them to python.
-
@cvp; works great, thank you.