iOSButton Back Color?

I’m sure this has come up before, but not finding it. I have the need for a few buttons to have a different back color and possibly the outline characteristics. I’m using iOSKit but not seeing anything in the project that would help.

am still baffled by the fact the Xojo neglected to expose such simple control properties… especially when the native UIButton control is SO flexible…

Thats one of the reason I use Xcode for creating iOS apps.

If I had that choice, I might have considered it. But the client required that we use Xojo. So I’m fulfilling their requirements.

Frankly, a comment like this is less than useful since I’m trying to find a solution in Xojo. Rewriting it in Xcode is NOT an option.

given the limited resources that “Xojo for iOS” currently provides, your only recourse seems to be a “roll your own” with a canvas .
that is the only alternative I could find on this forum…

For curiousity sake… .Why would the client care? Isn’t their goal to get a solution to a problem?

Because the client is already a Xojo shop.

You probably want to use the iOSButtonExtension.BackgroundColor from AppleLib (iOSLib):

https://github.com/UBogun/Xojo-AppleLib

Well, I tried to go down that route, but I’m already using iOSKit and have so many existing dependencies (including a delegate object) that switching over to AppleLib is going to cost me a bunch of time. That’s okay, I can revisit this later when the client complains that he hates the buttons. :slight_smile:

You can also do something similar independently (like, in a iOSButton’s open event, or your own method):

[code]Declare Function NSClassFromString Lib “Foundation” (className As CFStringRef) As Ptr
Declare Function colorWithRGBA Lib “UIKit” Selector “colorWithRed:green:blue:alpha:” ( UIColorClassRef As Ptr, red As CGFloat, green As CGFloat, blue As CGFloat, alpha As CGFloat) As Ptr
Declare Function view Lib “UIKit” Selector “view” (UIViewController As Ptr) As Ptr
Declare Sub setBackgroundColor Lib “UIKit” Selector “setBackgroundColor:” (UIView As Ptr, UIColor As Ptr)

Dim UIColorClassPtr As Ptr = NSClassFromString(“UIColor”)
Dim colorPtr As ptr = colorWithRGBA(UIColorClassPtr, (33 / 255), (209 / 255), (57 / 255), 1)
Dim viewPtr As Ptr = Me.Handle
setBackgroundColor(viewPtr, colorPtr)
[/code]

Travis for the win!

Note that in Travis’ code, “Declare Function view…” is useless for any iOSControl.

But it is useful if you want to set the background of an iOSView. In that case the code would be:

[code]Declare Function NSClassFromString Lib “Foundation” (className As CFStringRef) As Ptr
Declare Function colorWithRGBA Lib “UIKit” Selector “colorWithRed:green:blue:alpha:” ( UIColorClassRef As Ptr, red As CGFloat, green As CGFloat, blue As CGFloat, alpha As CGFloat) As Ptr
Declare Function view Lib “UIKit” Selector “view” (UIViewController As Ptr) As Ptr
Declare Sub setBackgroundColor Lib “UIKit” Selector “setBackgroundColor:” (UIView As Ptr, UIColor As Ptr)

Dim UIColorClassPtr As Ptr = NSClassFromString(“UIColor”)
Dim colorPtr As ptr = colorWithRGBA(UIColorClassPtr, (33 / 255), (209 / 255), (57 / 255), 1)
Dim viewPtr As Ptr = view(aView.Handle) //only change here
setBackgroundColor(viewPtr, colorPtr)[/code]

do you mean “not needed” ?
It is unused in his code but his code does work fine as is
But you can set the iosView background color - you need your code for that

That’s exactly what I meant Norman !

[quote=319723:@Travis Hill]Declare Function NSClassFromString Lib “Foundation” (className As CFStringRef) As Ptr
Declare Function colorWithRGBA Lib “UIKit” Selector “colorWithRed:green:blue:alpha:” ( UIColorClassRef As Ptr, red As CGFloat, green As CGFloat, blue As CGFloat, alpha As CGFloat) As Ptr
Declare Function view Lib “UIKit” Selector “view” (UIViewController As Ptr) As Ptr
Declare Sub setBackgroundColor Lib “UIKit” Selector “setBackgroundColor:” (UIView As Ptr, UIColor As Ptr)

Dim UIColorClassPtr As Ptr = NSClassFromString(“UIColor”)
Dim colorPtr As ptr = colorWithRGBA(UIColorClassPtr, (33 / 255), (209 / 255), (57 / 255), 1)
Dim viewPtr As Ptr = view(aView.Handle) //only change here
setBackgroundColor(viewPtr, colorPtr)[/quote]

i put this into a class for isoView on Open event. Wonder how i can make the color 33/209/255 variable

[code]Declare Function NSClassFromString Lib “Foundation” (className As CFStringRef) As Ptr
Declare Function colorWithRGBA Lib “UIKit” Selector “colorWithRed:green:blue:alpha:” ( UIColorClassRef As Ptr, red As CGFloat, green As CGFloat, blue As CGFloat, alpha As CGFloat) As Ptr
Declare Function view Lib “UIKit” Selector “view” (UIViewController As Ptr) As Ptr
Declare Sub setBackgroundColor Lib “UIKit” Selector “setBackgroundColor:” (UIView As Ptr, UIColor As Ptr)

Dim UIColorClassPtr As Ptr = NSClassFromString(“UIColor”)
Dim colorPtr As ptr = colorWithRGBA(UIColorClassPtr, (33 / 255), (209 / 255), (57 / 255), 1)
Dim viewPtr As Ptr = Me.Handle
setBackgroundColor(viewPtr, colorPtr)
[/code]

i put this into a class for iosButton on Open event. Wonder how i can make the color 33/209/255 variable

since both codes look similar, i create a method as follow but not sure what 2nd parameter i need to pass over to the method. vType is passed over from the calling code. What do i need to passed over to handle me.handle and view(self.handle)

		Declare Function NSClassFromString Lib "Foundation" (className As CFStringRef) As Ptr
		Declare Function colorWithRGBA Lib "UIKit" Selector "colorWithRed:green:blue:alpha:" ( UIColorClassRef As Ptr, red As CGFloat, green As CGFloat, blue As CGFloat, alpha As CGFloat) As Ptr
		Declare Function view Lib "UIKit" Selector "view" (UIViewController As Ptr) As Ptr
		Declare Sub setBackgroundColor Lib "UIKit" Selector "setBackgroundColor:" (UIView As Ptr, UIColor As Ptr)
		
		Dim UIColorClassPtr As Ptr =  NSClassFromString("UIColor")
		Dim colorPtr As ptr = colorWithRGBA(UIColorClassPtr, (222 / 255), (250 / 255), (226 / 255), 1)
		SELECT CASE vType
		CASE "Button"
				Dim viewPtr As Ptr = Me.Handle
		CASE "View"
				Dim viewPtr As Ptr = view(self.Handle) //only change here
		END SELECT
		setBackgroundColor(viewPtr, colorPtr)

Richard,
I assume you figured this out. Could you post your completed code for this method please? I am currently doing my best to figure it out myself.
Regards,
Phil

Add A Module to the application
Add a method to the module: e.g setDefaultBackground(Extends c As iOSControl), where “Extends c As iOSControl” is entered in the parameters…

setDefaultBackground(Extends c As iOSControl)

 Declare Function NSClassFromString Lib "Foundation" (className As CFStringRef) As Ptr
 Declare Function colorWithRGBA Lib "UIKit" Selector "colorWithRed:green:blue:alpha:" ( UIColorClassRef As Ptr, red As     CGFloat,green As CGFloat, blue As CGFloat, alpha As CGFloat) As Ptr

 Declare Function view Lib "UIKit" Selector "view" (UIViewController As Ptr) As Ptr
Declare Sub setBackgroundColor Lib "UIKit" Selector "setBackgroundColor:" (UIView As Ptr, UIColor As Ptr)

Dim UIColorClassPtr As Ptr =  NSClassFromString("UIColor")
Dim colorPtr As ptr = colorWithRGBA(UIColorClassPtr, (255 / 255), (251 / 255), (200 / 255), 1)
 Dim viewPtr As Ptr = c.Handle
setBackgroundColor(viewPtr, colorPtr)

// call this by putting in the Open eventhandler for (e.g. a button) (were the button is called resetButton)

resetbutton.setDefaultBackground

Thanks to those who pointed me in the right direction (RTFM)

now when using view, make the change as described:

// setViewBackground(Extends v As iOSView)

Declare Function NSClassFromString Lib “Foundation” (className As CFStringRef) As Ptr
Declare Function colorWithRGBA Lib “UIKit” Selector “colorWithRed:green:blue:alpha:” ( UIColorClassRef As Ptr, red As CGFloat, green As CGFloat, blue As CGFloat, alpha As CGFloat) As Ptr
Declare Function view Lib “UIKit” Selector “view” (UIViewController As Ptr) As Ptr
Declare Sub setBackgroundColor Lib “UIKit” Selector “setBackgroundColor:” (UIView As Ptr, UIColor As Ptr)

Dim UIColorClassPtr As Ptr = NSClassFromString(“UIColor”)
Dim colorPtr As ptr = colorWithRGBA(UIColorClassPtr, (255 / 255), (251 / 255), (230 / 255), 1)
Dim viewPtr As Ptr = view(v.Handle) //only change here
setBackgroundColor(viewPtr, colorPtr)

From the open action event for the view

setViewBackground() // all that is needed to change background for the view and buttons

[quote=379006:@Philip Cumpston]Richard,
I assume you figured this out. Could you post your completed code for this method please? I am currently doing my best to figure it out myself.
Regards,
Phil[/quote]

Sorry Phil,

i have not done anything lately on this… so can’t help you… but will try your code instead