iOS Question Changing colors of navbar and toolbar seems not to work anymore

schimanski

Well-Known Member
Licensed User
Longtime User
I compiled one of my projects new and it seems, that it is no longer possible to change the colors of the bars with the following code. I'm using hosted builder and set the MinVersion to 11:

B4X:
Dim no As NativeObject = NavControl
no.GetField("navigationBar").RunMethod("setBarStyle:",Array(1))
no.GetField("navigationBar").RunMethod("setTintColor:", Array(no.ColorToUIColor(Colors.Red)))    'Iconfarbe'
no.GetField("navigationBar").RunMethod("setBarTintColor:", Array(no.ColorToUIColor(Colors.Grey)))    'Hintergrundfarbe'
    
Dim no As NativeObject = TabControl
no.GetField("tabBar").SetField("tintColor", no.ColorToUIColor(Colors.Blue)) 'active item
no.GetField("tabBar").SetField("unselectedItemTintColor", no.ColorToUIColor(Colors.Green)) 'inactive item
no.GetField("tabBar").SetField("barTintColor", no.ColorToUIColor(Colors.Yellow)) 'bar color
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
There is a way to do it. Check the Pleroma source code. There are lots of stuffs Erel implemented in this particular app that is very useful.

 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Thanks, i found it and that seems to work:

B4X:
'Navbar:
    Dim no As NativeObject = NavControl
    no.GetField("navigationBar").RunMethod("setBarTintColor:", Array(no.ColorToUIColor(Colors.Grey))) 'Hintergrundfarbe'
    no.GetField("view").As(View).Color = Colors.Grey
    Dim no As NativeObject = App
    no.RunMethod("setStatusBarStyle:", Array(1))
    
'Tabbar
    Dim no As NativeObject = TabControl
    no.GetField("tabBar").SetField("tintColor", no.ColorToUIColor(Colors.Blue)) 'active item
    no.GetField("tabBar").SetField("unselectedItemTintColor", no.ColorToUIColor(Colors.White)) 'inactive item
    no.GetField("tabBar").SetField("barTintColor", no.ColorToUIColor(Colors.Grey)) 'bar color
    no.GetField("view").As(View).Color = Colors.Grey

But it seems to be kind of a different behavior. If I remember correctly, it was not possible until now to cover up the Navigationbar with a view:
Bild2.jpgbild1.jpg
 
Upvote 0

Michael Stewart

Member
Licensed User
I compiled one of my projects new and it seems, that it is no longer possible to change the colors of the bars with the following code. I'm using hosted builder and set the MinVersion to 11:

B4X:
Dim no As NativeObject = NavControl
no.GetField("navigationBar").RunMethod("setBarStyle:",Array(1))
no.GetField("navigationBar").RunMethod("setTintColor:", Array(no.ColorToUIColor(Colors.Red)))    'Iconfarbe'
no.GetField("navigationBar").RunMethod("setBarTintColor:", Array(no.ColorToUIColor(Colors.Grey)))    'Hintergrundfarbe'
   
Dim no As NativeObject = TabControl
no.GetField("tabBar").SetField("tintColor", no.ColorToUIColor(Colors.Blue)) 'active item
no.GetField("tabBar").SetField("unselectedItemTintColor", no.ColorToUIColor(Colors.Green)) 'inactive item
no.GetField("tabBar").SetField("barTintColor", no.ColorToUIColor(Colors.Yellow)) 'bar color

I've just spend 6 hours scratching my head why this stopped working. You're a lifesaver.
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I have spend some more time in changing the colors of bars, but i'm not able to set the toolbar to another color than the navigation bar. Since IOs 15 the toolbar is always colored in the same color as the navigation bar. Will it be possible again under IOS 15? Thanks or help...

B4X:
Private Sub Application_Start (Nav As NavigationController)
    
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)
    
    NavControl.NavigationBarVisible = True
    NavControl.ToolBarVisible=True
    
    'Color Navbar:
    Dim no As NativeObject = NavControl
    no.GetField("navigationBar").RunMethod("setBarTintColor:", Array(no.ColorToUIColor(Colors.Blue)))
    no.GetField("view").As(View).Color = Colors.Blue
    
    'Color Title Navbar:
    Dim no As NativeObject = App
    no.RunMethod("setStatusBarStyle:", Array(1))
    SetTitleColor(Nav, Colors.White)
    
    'Color Buttons Navbar:
    Dim no As NativeObject = NavControl
    no.GetField("navigationBar").RunMethod("setTintColor:", Array(no.ColorToUIColor(Colors.Red)))
    
    'Color Tollbar does not work:
    Dim no As NativeObject = NavControl
    no.GetField("toolbar").SetField("barTintColor", no.ColorToUIColor(Colors.gray))
    'no.GetField("view").As(View).Color = Colors.gray      <------ this line will color the tool- and the navigatonbar.
    

End Sub

Sub SetTitleColor(nav As NavigationController, clr As Int)
    Dim attributes As NativeObject
    attributes = attributes.Initialize("B4IAttributedString").RunMethod("createAttributes::", _
     Array(Font.CreateNew(18), attributes.ColorToUIColor(clr)))
    Dim no As NativeObject = nav
    no.GetField("navigationBar").RunMethod("setTitleTextAttributes:", Array(attributes))
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
iOS 15 - changing toolbar and nav bar colors:

B4X:
Private Sub SetColors(NavBarColor As Int, NavBarTextColor As Int, ToolBarColor As Int)
    Dim no As NativeObject = NavControl
    no.GetField("view").As(View).Color = ToolBarColor
    Dim BarAppearance As NativeObject
    BarAppearance = BarAppearance.Initialize("UINavigationBarAppearance").RunMethod("new", Null)
    BarAppearance.RunMethod("configureWithOpaqueBackground", Null)
    BarAppearance.RunMethod("setBackgroundColor:", Array(no.ColorToUIColor(NavBarColor)))
    Dim attributes As NativeObject
    attributes = attributes.Initialize("B4IAttributedString").RunMethod("createAttributes::", _
        Array(Font.CreateNew(18), attributes.ColorToUIColor(NavBarTextColor)))
    BarAppearance.RunMethod("setTitleTextAttributes:", Array(attributes))
    no.GetField("navigationBar").SetField("standardAppearance", BarAppearance)
    no.GetField("navigationBar").SetField("scrollEdgeAppearance", BarAppearance)
End Sub
 
Upvote 0
Top