UIButton / AppleButton BackgroundImage states

Ahoy there,

I’ve been trying to get a button to change it’s background image when clicked/tapped. I thought I had set up everything correctly, but I don’t see the background image changing, despite the button’s state changing. I was wondering if anyone else had come across this, or had suggestions for what might work?

Here’s the button initialisation:

[code] btn = new iOSButton
btn.NormalBackgroundImage = Load.loadiOSImage(“button-cancel-normal.png”)
btn.SelectedBackgroundImage = Load.loadiOSImage(“button-cancel-highlighted.png”)
btn.HighlightedBackgroundImage = Load.loadiOSImage(“button-cancel-highlighted.png”)
btn.DisabledBackgroundImage = Load.loadiOSImage(“button-cancel-highlighted.png”)

ab = btn.AppleButton
ab.AdjustsImageWhenHighlighted = True
ab.AdjustsImageWhenDisabled = True

ab.Enabled = True
ab.Highlighted = False
ab.Selected = False
dlog("init cmdrp3 state: " + enumToText(ab.State), CurrentMethodName)

btn.Name = “cmdRP3”

l = l + 67
t = 33
w = 66
h = 56
UI.setControlPos(btn, self, view, l, t, w, h)
AddHandler btn.Action, AddressOf clickedFourth[/code]

(Included the “selected” image for UIControlStateHighlighted, UIControlStateSelected, UIControlStateDisabled for testing, ideally, only Selected/Highlighted will be used).

And here’s the button action event:

dim ab as AppleButton = sender.AppleButton

If Not on Then
on = True
dlog(“on”, CurrentMethodName)

ab.Enabled = True
ab.Highlighted = True

on = false
dlog(“off”, CurrentMethodName)

ab.Enabled = True
ab.Highlighted = False

End If

Dlog("cur state: " + enumToText(sender.AppleButton.State), CurrentMethodName)[/code]

In the action event, I’ve tried setting AppleButton.Selected / AppleButton.Highlighted / AppleButton.Enabled on and off to see what changes it makes to the button.

Only disabling (ab.Enabled = False) the control changes the background image, which isn’t ideal, as in the disabled state the control can no longer be actioned. However, the log confirms that AppleButton.State is changing, in this case 0 (UIControlStateNormal) and 1 (UIControlStateHighlighted), which looks to be as intended, just no image change. :’(

Does anyone have any ideas for what might get this working as desired?

On further examination…

If ab.ImageForState(UIKitFramework.UIControlState.Highlighted) = nil Then break End If if ab.ImageForState(UIKitFramework.UIControlState.Selected) = nil then break end if

Both hit the breaks.

What extension / library are you using?
I just extended iOSLib’s button demo from the custom controls project with a me.HighlightedImage = iosLibLogo in the open event of the button and it works as intended.

Hey Ulrich,

Thanks for the reply. Whoops, sorry, please disregard my second post there. It should have been:

If ab.BackgroundImageForState(UIKitFramework.UIControlState.Highlighted) = nil Then break End If if ab.BackgroundImageForState(UIKitFramework.UIControlState.Selected) = nil then break end if

Which does NOT hit the breaks. Was getting a bit mixed up between BackgroundImage and just Image for Normal/Highlighted/Selected.

Changing the initialisation to

  btn.NormalImage = Load.loadiOSImage("button-cancel-normal.png")
  btn.SelectedImage = Load.loadiOSImage("button-cancel-highlighted.png")
  btn.HighlightedImage = Load.loadiOSImage("button-cancel-highlighted.png")

results in the button displaying a plane of blue, with no sign of the image unfortunately. Curses!

However, the BackgroundImageForState() are returning what look to be desired images, so they are getting set, but not being updated when the state is changing. (I tried Invalidating the control to see if that would update, but alas no).

This is using your iOSLib library, the iOSButtonExtension module and AppleButton classes.

What’s that load.loadiOSImage method? It’s not part of iOSLib (as far as I remember :wink: ) – have you tried something like btn.HighlightedImage = button-cancel-highlighted?

EDit: And I wonder about the blue plane. Is that color part of your pictures? Are they resized correctly (or in the other case could the blue be a part of the images instead)?

The load method is essentially just doing return iOSImage.FromFile(f).

Unfortunately the blue is definitely not part of any of the images, the images should be a greenish/olive colour.

I’ll try just adding the images to the project and setting them that way, see if that has any effect.

If it shouldn’t help, maybe it’s possible for you to strip down your project to just this part and send it to me. Or if you want to send your complete project which would be treated like under NDA of course. I’m sure we’ll find a fix for that issue.

Thanks Ulrich. I’ve PMed a project to you.

Did you ever get this to work? I want to do a small project with images on the buttons…


Makes me wonder what the project was – I couldn’t find any private messages with Chris …??!
Anyway: although iOSLib is a mess currently and I don’t know when I’ll find time to clean it up, there was the buttonExtension which still should be working – although I am unsure what the problem discussed here was (and if I ignored Chris’ pm unintentionally). Just ignore the missing interface. Sorry, the last upload was done in a hurry and I didn’t realize for some time it created a very unordered state.

Thank you Ulrich,
What I actually want to do is have a grid of 5x7 buttons on One ipad page. Each button would be 125x125 pixels with a portait picture of a person as the button face. I really do not need a down button picture. I started to expreriment with segments controlls using seven controlls each with five segments, but getting the images in seems to be an issue with the 22x22 and 44x44 size limits. The pictures and order may change based on a read from a data file on a server.

Any thought on a direction?

Thank You,

Do you want to show anything more than the pictures? Sounds to me like a canvas or imagewell together with their tap events would do too, or wouldn’t they?

I moved on to another program and got the software out the door. I will come back to this another day.


Ulrich… no declare in your magic toolbox to implement the UIButton image attribute?

Oh sorry Dave, I completely missed your question. Yes, AppleButton/UIButton is complete in iOSLib. What is not finished is the iOSLibButton custom control – I did not implement the image properties yet. But you can access them via the AppleObject As AppleButton, either in a custom control or in a normal Xojo iOSButton.