Button and view colours (iOS)

How do I change the background color of a view in iOS? Or change the color of a button?
Strangely, buttons are white-on-white by default, making them invisible.

You can put a rectangle that fills the view.
Buttons on iOS8 is by default “only a blue text” :slight_smile:

iOS 8 buttons have an invisible / white background by default, and only show the textual caption.

Hmm, I could have sworn someone asked a question about an invisible button??
I must be losing it :slight_smile:

[quote] How do I change the background color of a view in iOS? Or change the color of a button?
Strangely, buttons are white-on-white by default, making them invisible.
[/quote]

Sorry I accidentally deleted the question. Thanks for the answer.
And what about the view itself? Do I need to put a rectangle, or can the background color be changed?

There might be some declare for that.
I aint no declare master though…
I bet they are around here somewhere. Declare masters are sometimes a little shy :wink:

[quote=151539:@Vincent Verweij]Sorry I accidentally deleted the question. Thanks for the answer.
And what about the view itself? Do I need to put a rectangle, or can the background color be changed?[/quote]

The view has no backcolor or backdrop properties. You must use a rectangle, a canvas or an imageView to display solid colors or pictures.

Not sure how Xojo-iOS is implementing buttons… but the UIButton object does in fact have background color (and/or image) properties as well as font and font color properties… I’d be very disappointed if XOJO did not (or was not planning) on exposing those to the developer.

This was filed already. But I think, Dave, you should add what you just posted.

37047 - Would like to be able to set iOSButton Background Color and Border
Status: Reviewed Rank: Not Ranked Product: Xojo Category: IDE » Inspector

Yousaf Shah on Dec 1, 2014 at 11:25 PM
Would be great to be able to set the background colour of the IOSbutton.

Especially since you can nicely size it but the user cannot see its bounds! Not always necessary but when doing buttons such as Reset and those new style full-width buttons at the footer of layouts.

Being able to se the border width and colour would be useful also.

<https://xojo.com/issue/37047>

Pending the eventual implementation of background color and border, a rectangle can do fine. And an ImageView for pictures. Fonts are a little trickier, but using a canvas DrawTextLine and it’s PointerDown event can do a decent job.

Yes, it is a workaround, but when one has no fork, one who is hungry eats with one’s fingers instead of starving to death :wink:

Here’s a method to add a round rect to an iOSButton

[code]Sub setColor(extends b As iOSButton, back As color, border as color, radius as double)
declare sub setBackgroundImage lib “UIKit” selector “setBackgroundImage:forState:” (obj as ptr, value as ptr, state as integer)

radius=min(radius,min(b.Width,b.Height)/2-.01)

dim pth As new iOSPath
pth.AddRoundRect(0,0,b.width,b.height,radius,radius)

dim p as new iOSBitmap(b.Width,b.Height,1)
p.Graphics.LineColor=border
p.Graphics.FillColor=back
p.Graphics.LineWidth=1
p.Graphics.FillPath(pth)
p.Graphics.DrawPath(pth)

dim i As iOSImage=p.Image
setBackgroundImage(b.Handle,i.Handle,0)
End Sub
[/code]

[quote=151564:@jim mckay]Here’s a method to add a round rect to an iOSButton

[code]Sub setColor(extends b As iOSButton, back As color, border as color, radius as double)
declare sub setBackgroundImage lib “UIKit” selector “setBackgroundImage:forState:” (obj as ptr, value as ptr, state as integer)

radius=min(radius,min(b.Width,b.Height)/2-.01)

dim pth As new iOSPath
pth.AddRoundRect(0,0,b.width,b.height,radius,radius)

dim p as new iOSBitmap(b.Width,b.Height,1)
p.Graphics.LineColor=border
p.Graphics.FillColor=back
p.Graphics.LineWidth=1
p.Graphics.FillPath(pth)
p.Graphics.DrawPath(pth)

dim i As iOSImage=p.Image
setBackgroundImage(b.Handle,i.Handle,0)
End Sub
[/code][/quote]

If that is alright with you, Jim, I’ll like to add that method to XojoiOSWrapper. Since SetColor is a bit too generic, what about renaming it SetButtonColor ?

Sure… how about setBackgroundRoundedRectangle

Here’s another quicky…

Sub backdrop(extends b As iOSButton, assigns backdrop as iOSImage) declare sub setBackgroundImage lib "UIKit" selector "setBackgroundImage:forState:" (obj as ptr, value as ptr, state as integer) setBackgroundImage(b.Handle,backdrop.Handle,0) End Sub

mybutton.backdrop=myimage

[quote=151568:@jim mckay]Sure… how about setBackgroundRoundedRectangle

Here’s another quicky…

Sub backdrop(extends b As iOSButton, assigns backdrop as iOSImage) declare sub setBackgroundImage lib "UIKit" selector "setBackgroundImage:forState:" (obj as ptr, value as ptr, state as integer) setBackgroundImage(b.Handle,backdrop.Handle,0) End Sub

mybutton.backdrop=myimage[/quote]

Great work, Jim. setBackgroundRoundedRectangle it is, and am going to add backdrop as well.

Thank you.

Jim shows his skills once again :wink:
Great work!

I have an issue with backdrop. Here is the code I used in a button Action event :

dim pic as iOSImage dim f as FolderItem f = SpecialFolder.GetResource("blue.jpeg") pic = pic.FromFile(f) ImageView1.Image = pic me.Backdrop(pic)

The picture does display fine in the ImagevIew so I know pic is a valid iOSImage, but I get

Assignement accessor must be invoked by assigning a value me.backdrop(pic)

Any idea ?

try me.backdrop=pic

That works. Thank you Jim.

Thanks for the code, Jim. But I’m not sure how to use it. Where do I add it?

[quote=151816:@Vincent Verweij]Thanks for the code, Jim. But I’m not sure how to use it. Where do I add it?

[/quote]
It needs to be in a module with global scope.

(for anyone who needs a step-by-step)

  1. In a project select “Module” from the “Insert” menu in the IDE
  2. Name the Module, “IOSExtensions” would be a fine name
  3. Select Insert-Method, name the method and set the parameters
  4. Add the code.
  5. Be sure to set the scope to global in order to extend iOSControl