iOS Question Image on SegmentedControl

ciginfo

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

Erel

B4X founder
Staff member
Licensed User
Longtime 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: https://www.b4x.com/android/forum/threads/barbutton-image-color.52365/#post-328103
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime 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")))
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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

SS-2015-04-22_10.27.29.png
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime 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"))))
 
Upvote 0

Brian Robinson

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

You need to call KeepOriginalColors again for the second bitmap.

Cheers
Brian
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime 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"))) )
 
Upvote 0

Brian Robinson

Active Member
Licensed User
Longtime 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
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime User
Here small project joined. Parsing error
I want a segmented control with 3 items and an a different image on every item.
Images are attached because they no enter into the zip
 

Attachments

  • segmented.zip
    2.5 KB · Views: 345
  • autour_moi.png
    autour_moi.png
    3 KB · Views: 316
  • landes.png
    landes.png
    4.7 KB · Views: 326
  • pays_basque.png
    pays_basque.png
    5.1 KB · Views: 312
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime 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"))))
 
Upvote 0

Brian Robinson

Active Member
Licensed User
Longtime 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
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0

ciginfo

Well-Known Member
Licensed User
Longtime 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.
 
Upvote 0
Top