# UIButton / AppleButton BackgroundImage states

**URL:** <https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464>\
**Category:** iOS\
**Created:** [October 22, 2015, 12:03pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464 "2015-10-22T12:03:02Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris\_drysdale](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/bc79bd/32.png) [@chris\_drysdale](https://forum.xojo.com/u/chris_drysdale)\
**Post date:** [October 22, 2015, 12:03pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/1 "2015-10-22T12:03:02Z")

</div>

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”  
self.AddControl(btn)

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:

[code]  
dim ab as AppleButton = sender.AppleButton

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

```
ab.Enabled = True
ab.Highlighted = True

```

Else  
on = false  
dlog(“off”, CurrentMethodName)

```
ab.Enabled = True
ab.Highlighted = False

```

End If

[//sender.Invalidate](https://sender.Invalidate)  
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?

---

<div class="post-metadata">

**Author:** ![chris\_drysdale](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/bc79bd/32.png) [@chris\_drysdale](https://forum.xojo.com/u/chris_drysdale)\
**Post date:** [October 22, 2015, 12:15pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/2 "2015-10-22T12:15:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [October 22, 2015, 12:27pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/3 "2015-10-22T12:27:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chris\_drysdale](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/bc79bd/32.png) [@chris\_drysdale](https://forum.xojo.com/u/chris_drysdale)\
**Post date:** [October 22, 2015, 1:24pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/4 "2015-10-22T13:24:13Z")

</div>

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

```auto
  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.

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [October 22, 2015, 1:34pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/5 "2015-10-22T13:34:53Z")

</div>

What’s that load.loadiOSImage method? It’s not part of iOSLib (as far as I remember 😉 )  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)?

---

<div class="post-metadata">

**Author:** ![chris\_drysdale](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/bc79bd/32.png) [@chris\_drysdale](https://forum.xojo.com/u/chris_drysdale)\
**Post date:** [October 22, 2015, 1:50pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/6 "2015-10-22T13:50:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [October 22, 2015, 1:59pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/7 "2015-10-22T13:59:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chris\_drysdale](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/bc79bd/32.png) [@chris\_drysdale](https://forum.xojo.com/u/chris_drysdale)\
**Post date:** [October 22, 2015, 2:38pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/8 "2015-10-22T14:38:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![James\_M\_Wadkins](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@James\_M\_Wadkins](https://forum.xojo.com/u/James_M_Wadkins)\
**Post date:** [August 19, 2016, 2:12am UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/9 "2016-08-19T02:12:04Z")

</div>

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

THX  
JMW

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [August 19, 2016, 1:50pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/10 "2016-08-19T13:50:24Z")

</div>

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.  
 ![](https://dl.dropboxusercontent.com/u/21200221/Xojo/iOSLib%20Button.png)

---

<div class="post-metadata">

**Author:** ![James\_M\_Wadkins](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@James\_M\_Wadkins](https://forum.xojo.com/u/James_M_Wadkins)\
**Post date:** [August 19, 2016, 2:16pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/11 "2016-08-19T14:16:15Z")

</div>

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,  
JMW

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [August 19, 2016, 6:10pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/12 "2016-08-19T18:10:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![James\_M\_Wadkins](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@James\_M\_Wadkins](https://forum.xojo.com/u/James_M_Wadkins)\
**Post date:** [August 23, 2016, 12:03am UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/13 "2016-08-23T00:03:05Z")

</div>

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

Thx  
JMW

---

<div class="post-metadata">

**Author:** ![DaveS](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@DaveS](https://forum.xojo.com/u/DaveS)\
**Post date:** [August 23, 2016, 12:06am UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/14 "2016-08-23T00:06:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [November 6, 2016, 6:25pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/15 "2016-11-06T18:25:04Z")

</div>

> [@283347:@Dave S](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:01pm UTC](https://forum.xojo.com/t/uibutton-applebutton-backgroundimage-states/27464/16 "2020-10-29T18:01:11Z")

</div>


