En una entrada previa hemos visto como colorear la NavigationBar en una MobileScreen para los proyectos iOS; pero el hecho de definir un color para dicho elemento significa que deberíamos de tener más control a la hora de ajustar también el color tanto para el Título mostrado en la barra de navegación como en los botones (opcionales) que podamos añadir en la misma. Continúa leyendo y te mostraré como podemos hacerlo.
Como en el anterior artículo, tendremos que utilizar varios Declares para llamar a las funciones y sub-rutinas requeridas en el Framework de iOS, de modo que podamos acceder y modificar los valores en los objetos subyacentes. Si aún no has seguido el anterior artículo, tendrás que hacerlo dado que ahora añadiremos nuevos métodos y propiedades sobre el mismo proyecto de ejemplo utilizado en el artículo previo.
Por tanto, añadamos un nuevo método en el Módulo “DeclaresForiOS” creado previamente, utilizando para ello los siguientes valores:
- Method Name: NavigationBarTextColor
- Parameters: Extends screen As MobileScreen, Assigns value As ColorGroup
- Scope: Global
Y escribe el siguiente fragmento de código en el Editor de Código asociado:
if value = nil then Return var controller as ptr = screen.ViewControllerHandle Declare Function navigationController Lib "UIKit" Selector "navigationController" (controller as ptr) as ptr Declare Function navigationBar Lib "UIKit" Selector "navigationBar" (controller as ptr) as ptr var nc as ptr = navigationController(controller) var nv as ptr = navigationBar(nc) Var colPtr As ptr colptr = ColorGroupToUIColor(value) Declare Function dictionaryWithObjectForKey Lib "UIKit" Selector "dictionaryWithObject:forKey:" (obj as ptr, value as ptr, key as CFStringRef) as ptr // https://developer.apple.com/documentation/foundation/nsclassfromstring(_:)?language=objc Declare Function NSClassFromString Lib "Foundation" (name As CFStringRef) As Ptr Declare Function standardAppearance Lib "UIKit" Selector "standardAppearance" (obj As ptr) As ptr Declare Sub setStandardAppearance Lib "UIKit" Selector "setStandardAppearance:" (obj as ptr, value as ptr) Declare Sub setScrollEdgeAppearance Lib "UIKit" Selector "setScrollEdgeAppearance:" (obj as ptr, value as ptr) Declare Sub setTitleTextAttributedText Lib "UIKit" Selector "setTitleTextAttributes:" (obj as ptr, value as ptr) Declare Sub SetLargeTitleTextAttributedText Lib "UIKit" Selector "setLargeTitleTextAttributes:" (obj as ptr, value as ptr) Declare Sub SetTintColor Lib "UIKit" Selector "setTintColor:" (obj as ptr, value as ptr) // We need to create a NSDictionary with the attribute we want to set on text for the NavigationBar Appearance var dict as ptr = dictionaryWithObjectForKey(NSClassFromString("NSMutableDictionary"), colPtr, "NSColor") var appear as ptr = standardAppearance(nv) // Setting the new appearance settings both for the regular-sized title // and the Large one SetTitleTextAttributedText(appear, dict) SetLargeTitleTextAttributedText(appear, dict) // And we apply the modified appearance to the StandardAppearance setStandardAppearance(nv, appear) // And to the scrollEdgeAppearance if the app is run on iOS 15+ if (System.Version.MajorVersion >= 15.0) then setScrollEdgeAppearance(nv, appear) end if // The TintColor is applied on the text of the added buttons to the NavigationBar // So we need to use the same color for them! SetTintColor(nv, colptr)
Y este es todo el código que necesitaremos.
¡Coloreando!
Selecciona el elemento Screen1 en el Navegador y añade una nueva propiedad utilizando los siguientes valores:
- Name: mNavigationBarTextColor
- Type: ColorGroup
- Scope: Protected
Selecciona ahora el evento Screen1.Opening y añade la siguiente línea de código:
mNavigationBarTextColor = new ColorGroup( color.Yellow, color.red )
Por último, selecciona el manejador de evento Screen1.AppearanceChanged y añade la siguiente línea de código:
me.NavigationBarTextColor = mNavigationBarTextColor
Ejecuta el proyecto de ejemplo y, ahora, verás cómo el texto mostrado en el Título es el asignado incluso cuando se cambia el dispositivo (o Simulador) entre los modos Claro y Oscuro.