iOS Question Image on SegmentedControl

ciginfo

Well-Known Member
Licensed User
HI,
Is there any way to put a little icon into a SegmentedControl item?
Thank you
 

Erel

Administrator
Staff member
Licensed User
Yes:
B4X:
Private Sub Application_Start (Nav As NavigationController)   
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.RootPanel.LoadLayout("1")
   NavControl.ShowPage(Page1)
   SetImageItems(SegmentedControl1, Array (LoadBitmap(File.DirAssets, "image1.png"), _
     LoadBitmap(File.DirAssets, "image2.png")))
End Sub
Sub SegmentedControl1_IndexChanged (Index As Int)
   Log(Index)
End Sub

Sub SetImageItems(sc As SegmentedControl, Images As List)
   Dim no As NativeObject = sc
   no.RunMethod("removeAllSegments", Null)
   Dim index As Int = 0
   For Each bmp As Bitmap In Images
     no.RunMethod("insertSegmentWithImage:atIndex:animated:", _
       Array(bmp, index, False))
     index = index + 1
   Next
End Sub
If you want to keep the images colors: http://www.b4x.com/android/forum/threads/barbutton-image-color.52365/#post-328103
 

ciginfo

Well-Known Member
Licensed User
Like this to keep the images colors? But it doesn't work.
B4X:
SetImageItems(segmentedcontrol1, Array KeepOriginalColors(LoadBitmap(File.DirAssets, "image1.png"), _
     LoadBitmap(File.DirAssets, "image2.png"),LoadBitmap(File.DirAssets, "image3.png")))
 

Erel

Administrator
Staff member
Licensed User
It works here:
B4X:
Private Sub Application_Start (Nav As NavigationController)   
   NavControl = Nav
   Page1.Initialize("Page1")
   Page1.RootPanel.LoadLayout("1")
   NavControl.ShowPage(Page1)
   SetImageItems(SegmentedControl1, Array(KeepOriginalColors(LoadBitmap(File.DirAssets, "smiley.png"))))
End Sub

Sub KeepOriginalColors(bmp As Bitmap) As Bitmap
  Dim no As NativeObject = bmp
  Return no.RunMethod("imageWithRenderingMode:", Array(1))
End Sub
 

ciginfo

Well-Known Member
Licensed User
Yes, but if I have a SegmentedControl with 2 items or more, compiling error. It seems '(' expected but I don't see where.
Here my code for 2 items.
B4X:
SetImageItems(SegmentedControl1, Array(KeepOriginalColors(LoadBitmap(File.DirAssets, "smiley.png"),LoadBitmap(File.DirAssets, "smiley2.png"))))
 

Brian Robinson

Active Member
Licensed User
KeepOriginalColors is expecting a bitmap not an array of bitmaps.

You need to call KeepOriginalColors again for the second bitmap.

Cheers
Brian
 

ciginfo

Well-Known Member
Licensed User
I don't understand, these 2 codes do not work
1
B4X:
SetImageItems(segmentedControl1, Array(KeepOriginalColors(LoadBitmap(File.DirAssets, "smiley1.png"))), _
     Array(KeepOriginalColors(LoadBitmap(File.DirAssets, "smiley2.png"))) )
2
B4X:
SetImageItems(segmentedControl1, Array(KeepOriginalColors(LoadBitmap(File.DirAssets, "smiley1.png"))), _
     (KeepOriginalColors(LoadBitmap(File.DirAssets, "smiley2.png"))) )
 

Brian Robinson

Active Member
Licensed User
The second piece of code is very close. The brackets just aren't in the right place. Actually one set of brackets too many.

The code is expecting SetImageItems(segmentedControl1, Array(bitmap,bitmap))

B4X:
SetImageItems(segmentedControl1, Array( _
            KeepOriginalColors(LoadBitmap(File.DirAssets, "smiley1.png"))), _ '
             (KeepOriginalColors(LoadBitmap(File.DirAssets, "smiley2.png"))) )
If you copy this code into your app and put the cursor on the ending brackets it will highlight the staring bracket and you will see where the problem is.

Cheers
 

ciginfo

Well-Known Member
Licensed User
Ok for dash instead of underscore but I don't see problem with bbrackets in my code below
B4X:
SetImageItems(SegmentedControl1, Array(KeepOriginalColors(LoadBitmap(File.DirAssets, "landes.png"))), _ '
             (KeepOriginalColors(LoadBitmap(File.DirAssets, "autour_moi.png"))), _ '
             (KeepOriginalColors(LoadBitmap(File.DirAssets, "pays_basque.png"))))
 

Brian Robinson

Active Member
Licensed User
The third closing bracket on the top line is closing your array.

Put the cursor on the last closing bracket of the first line. You will see it is the closing bracket for the Array. You want an Array of bitmaps whereas you are currently passing SetImageItems(segmentedControl,Array,Bitmap,Bitmap)

Note you do not need the brackets around the KeepOriginalColors call.

cheers
 

ciginfo

Well-Known Member
Licensed User
Thank you for all the time you spend with my problem. But can you correct my code in th small project posted above, so that it works. Despite your explanations I can not.
Thank you very much.
 

ciginfo

Well-Known Member
Licensed User
Thank you, now I understand. My poor knowledge of English did not help me to understand. With an example that is much simpler. Thank you again.
 
Top