Changing navigationbar color

Using the below declares you can change the color of the top/navigationbar (thanks for Michel for putting this in the iOSWrapper).

But this only seems to work when triggered from for example a button.
If you put this in an open event of an iOSView - so the color is immediately changed on showing the iOSView, it does not work.
How come?

[code]declare function NSClassFromString lib “Foundation” (classname as CFStringRef) as ptr
declare function keyWindow lib “UIKit” selector “keyWindow” (obj_ref as ptr) as ptr
declare function sharedApplication lib “UIKit” selector “sharedApplication” (obj_ref as ptr) as ptr
declare function rootViewController lib “UIKit” selector “rootViewController” (obj_ref as ptr) as ptr
declare function navigationBar lib “UIKit” selector “navigationBar” (obj_ref as ptr) as ptr

dim sApp as ptr=sharedApplication(NSClassFromString(“UIApplication”))
dim currentWindow as ptr=keyWindow(sApp)
dim navController as ptr=rootViewController(currentWindow)
dim navBar as ptr= navigationBar(navController)

Declare Sub setBarTintColor lib UIKit selector “setBarTintColor:” (id as ptr, UIColor as Ptr)
setBarTintColor navBar, UIColor(&cFF0000)

declare Sub setTintColor lib UIKit selector “setTintColor:” (id as ptr, UIColor as Ptr)
setTintColor navBar, UIColor(&cFF7700)

declare sub setTranslucent lib UIKit selector “setTranslucent:” (id as ptr)
setTranslucent navBar[/code]

This is the error when it crashes:

Application Specific Information:
*** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[UITabBarController navigationBar]: unrecognized selector sent to instance 0xca6db40’
abort() called
CoreSimulator 110.4 - Device: iPhone 5 - Runtime: iOS 8.1 (12B411) - DeviceType: iPhone 5
terminating with uncaught exception of type NSException

It probably crashes because in Open the bar does not exist.

I tried the code in Activate, where it does not crashes, but does nothing. I guess it is because at that time, the bar is not completely displayed yet.

It works fine in the Action event of a 100 ms single timer.

Ok, tried this and worked in the emulator.
On my iPhone5 it works too. However it does not on my older iPhone4s (just crashes)
I guess using timers can be troublesome on some slower devices.
Also tried with a delay of 1000 but same crash.

Guess we have to wait for Xojo implementing this by default (among a lot of other things :slight_smile:

[quote=155533:@Christoph De Vocht]Ok, tried this and worked in the emulator.
On my iPhone5 it works too. However it does not on my older iPhone4s (just crashes)
I guess using timers can be troublesome on some slower devices.
Also tried with a delay of 1000 but same crash.

Guess we have to wait for Xojo implementing this by default (among a lot of other things :)[/quote]

Is the iPhone 4s under iOS 8 ? Could be a question of system version.

According to the reference, though, barTintColor is available system 2 onward.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationBar_Class/

You may also want to comment out

setTintColor navBar, UIColor(&cFF7700) setTranslucent navBar

If all you want is to change only the backcolor, and translucent is the default anyway.

Unless Xojo moved things around…
For a UINavigationBar you should

a) use [backgroundColor] for the background
b) use [tintColor] to change the color of the Left/Right buttons (if any)
c) use [titleTextAttributes] to change the color of the Center Title

at least those are the only ways I get it to work in objC/Swift

[barTintColor] is there, but has no effect it seems

[quote=155545:@Dave S]Unless Xojo moved things around…
For a UINavigationBar you should

a) use [backgroundColor] for the background
b) use [tintColor] to change the color of the Left/Right buttons (if any)
c) use [titleTextAttributes] to change the color of the Center Title

at least those are the only ways I get it to work in objC/Swift

[barTintColor] is there, but has no effect it seems[/quote]

These declares use exactly the same terminology as Apple does in its framework reference. No Xojo here.

If you look at the UINavigation Class Reference on the iOS Developer Library I posted the link to, there is no [backgroundColor]. BarTintColor does change the bar back color the same way.

I do not see why it should crash specifically on the 4s, since these properties have been in use since iOS 2.00.

The iPhone4s has iOS7 installed.

But it keeps crashing. When I change the background with a button (as in the iOSWrapper example) it does work.

[quote=155598:@Christoph De Vocht]The iPhone4s has iOS7 installed.

But it keeps crashing. When I change the background with a button (as in the iOSWrapper example) it does work.[/quote]

You say a 1000 timer still crashes. Could it not be enough, since the 4s has to be significantly slower than the simulator ? Taping the button usually takes two seconds or so. Could it be that ?

Otherwise I do not see what can explain the crash, since the declare applies the iOS library to the letter. Which means if Xojo implemented the property, it would do exactly the same thing.

My first thought was the same but even putting the timer to 10 seconds it crashed.
I have no idea why this happens.

Fingers crossed if Xojo can implement this in the next version by default. :slight_smile:

You want to set these color on view basis or globally (all the views?)
if you want set globally you can use appearance methods (declare) in the app.open event

atm I only need to change the navigationbar (top) color.

Jason Tait filed a FR in beta <https://xojo.com/issue/36806> so Xojo is already aware of that need.

I know you do not like Xojo solutions, just declares :wink:

Yet, the solution could be not to apply the declare when the screen size is 4s, and instead pushto a 4s view where the navigation bar is simulated with a colored rectangle and a label. I just verified it works, too.

I would do that for my own app. But hey, I also scroll within a canvas with timer animations, and you call that tricks …