Use an image in place of the NavigationBar image

Hi all,

I was able to set the NavigationBar color using the code from another thread here.

But I’m struggling to use a picture as NavigationBar Title.

[code]Public Sub setNavBarTitleImage(extends v as iOSView, image As iOSImage)

declare function navigationController lib “UIKit” selector “navigationController” (viewController as ptr) as ptr
declare function navigationBar lib “UIKit” selector “navigationBar” (obj_ref as ptr) as ptr
declare function topItem lib UIKitLib selector “topItem” (id as ptr) as ptr
declare sub setTitleView lib UIKitLib selector “setTitleView:” (id as ptr, UIImage as Ptr)

dim navigationControllerRef as ptr = navigationController(v.ViewControllerHandle)

dim navBar as ptr = navigationBar(navigationControllerRef)

dim navItem as ptr = topItem(navBar)

//Code crashes here, although navItem isn’t &h0000000 in the debugger
setTitleView(navItem, image.Handle)

End Sub
[/code]

The first parameter of setTitleView is navItem is supposed to be the UIView representing the NavigationBar Title view.

The second parameter is a UIImage reference.

Thanks Jean-Paul, I understand better now.

I was using some Swift code I found on Github and some examples showed directly a reference to the UIImage. I might have misunderstood the code.

you have to add before the last instruction :
declare Function NSClassFromString lib “Foundation”(className as CFStringRef) as Ptr
declare Function alloc lib “Foundation” selector “alloc”(classPtr as Ptr) as Ptr
declare Function initWithImage lib “UIKit” selector “initWithImage:”(objRef as Ptr, imgRef as Ptr) as Ptr

dim iView as Ptr= initWithImage( alloc( NSClassFromString(“UIImageView”)), image.Handle)

and then call the setTitleView with the right item
setTitleView(navItem, iView)

Thank you both for your help.

This is the final code that works perfectly:

[code]Public Sub setNavBarTitleImage(extends v as iOSView, image As iOSImage)

declare function navigationController lib “UIKit” selector “navigationController” (viewController as ptr) as ptr
declare function navigationBar lib “UIKit” selector “navigationBar” (obj_ref as ptr) as ptr
declare function topItem lib UIKitLib selector “topItem” (id as ptr) as ptr
declare sub setTitleView lib UIKitLib selector “setTitleView:” (id as ptr, UIImage as Ptr)
declare Function alloc lib “Foundation” selector “alloc”(classPtr as Ptr) as Ptr
declare Function initWithImage lib “UIKit” selector “initWithImage:”(objRef as Ptr, imgRef as Ptr) as Ptr

//Reference to Navigation Controller
dim navigationControllerRef as ptr = navigationController(v.ViewControllerHandle)

//Ref to NavigationBar
dim navBar as ptr = navigationBar(navigationControllerRef)

//Ref to Title item
dim navItem as ptr = topItem(navBar)

//Create imageView with the passed image
dim iView as Ptr = initWithImage( alloc( NSClassFromString(“UIImageView”)), image.Handle)

//Set Title item to use the imageView
setTitleView(navItem, iView)
End Sub
[/code]