How to set default colours with declare to appearance proxy?

In a couple of different threads relating to setting background colour there is mention to declare the “appearance” in order to set the default colour for all instances of a control, or I imagine a view background. I have not seen anything yet on how to actually do this. I have been looking at the Apple reference for UIAppearance and some declare examples, but I don’t have any experience writing declares so it doesn’t make sense yet.

I found the declares for setting the background colour of a view and have tried that. While this works to set each view when opened I thought it would be easier to do it only once for all views globally by setting the appearance proxy.

Has this already been done anywhere? I’m watching the webinar on declares again and trying to figure this out. Should this be fairly simple to do or should I start with something easier?


I got something working for setting the navigation bar proxy background colour so all views navigation bars are created with the colour I set. I’m able to call this once in the app open event and it seems to work well. Here is the code I am using. I don’t know if there is a better way so any comments on how to improve would be welcome.

[code] Declare Function NSClassFromString Lib “Foundation” (classname As CFStringRef) As Ptr

// Create pointer to UINavigationBar
Dim navBarPtr As Ptr = NSClassFromString(“UINavigationBar”)
// Get a proxy to the class
Declare Function GetAppearanceProxy lib “UIKit” selector “appearance” (UINavigationBar As Ptr) As Ptr
Dim navBarProxy As Ptr = GetAppearanceProxy(navBarPtr)

// Create the colour
Dim theUIColorClassRef As Ptr = NSClassFromString(“UIColor”)
Declare Function GetColorWithRGBA lib “UIKit” selector “colorWithRed:green:blue:alpha:” (UIColorClassRef As Ptr, red As Single, green As Single, blue As Single, alpha As Single) As Ptr
Dim myUIColorObject As ptr = GetColorWithRGBA(theUIColorClassRef, (139.0/255.0), (172.0/255.0), (165.0/255.0), 1.0)

Declare Sub SetDefaultColour lib “UIKit” selector “setBarTintColor:” (UINavigationBar As Ptr, myNavBarBackgroundColor As Ptr)
SetDefaultColour(navBarProxy, myUIColorObject)


I wanted to add code to set up the background colour for all table cell views. I thought I could do the same as above but it is not working. Here is the code I added.

[code] // Create pointer to view
Dim viewPtr As Ptr = NSClassFromString(“UIView”)
// Get a proxy to the class
Declare Function GetAppearanceProxy2 lib “UIKit” selector “appearance” (UIView As Ptr) As Ptr
Dim viewProxy As Ptr = GetAppearanceProxy2(viewPtr)

// Create the colour
myUIColorObject = GetColorWithRGBA(theUIColorClassRef, (213.0/255.0), (223.0/255.0), (229.0/255.0), 1.0)

Declare Sub SetDefaultColour2 lib “UIKit” selector “setBackgroundColor:” (UIView As Ptr, myBackgroundColor As Ptr)
SetDefaultColour2(viewProxy, myUIColorObject)

This added code sets the colour on the whole screen and covers over everything. So you can’t see the title, any rows, etc. I tried changing the code to “setTintColor:” instead, but that didn’t do anything.

Is there a way to set the table cells proxy so the tables have the background colour I want?

The table cells are objects inside the table. What you changed is the color of the core table view, therefore everything went colorful.

Changing the colors on a table cell isn’t that easy, because Apple caches the cells for faster recreation. So in my experiments this was unreliable, the table cells de-colored themselves from time to time. But maybe your idea of setting the appearance defaults could turn out very useful.

You can get the cells as UITableviewcells via cellForRowAtIndexPath (but have to create a NSIndexPath object before that contains the desired cell, or you can get the currently visible cells via visibleCells, both from inside the UItableview object which is the representation of a iosTable. On the UITableview cell which is a subclass of UIView, the setbackgroundcolor should work again – but with the limitations I wrote about above – or maybe addressing their contentView property turns out to be more useful. Would be glad to hear if you have any success.