Can I set image as a title to UINavigationBar?

后端 未结 16 1908
有刺的猬
有刺的猬 2020-12-07 09:38

Is it possible to set some image as title of Navigation bar?

I think NYTimes application used a Navigation bar and title is look like image file (the reason why it\'

16条回答
  •  半阙折子戏
    2020-12-07 10:23

    For those who have the same error but in Xamarin Forms, the solution is to create a Renderer in iOS app and set the image like so :

    [assembly: Xamarin.Forms.ExportRenderer(typeof(Xamarin.Forms.Page), typeof(MyApp.Renderers.NavigationPageRenderer))]
    namespace MyApp.Renderers
    {
        #region using
    
        using UIKit;
        using Xamarin.Forms.Platform.iOS;
    
        #endregion
    
        public class NavigationPageRenderer : PageRenderer
        {
            public override void ViewDidLoad()
            {
                base.ViewDidLoad();
                SetTitleImage();
            }
    
            private void SetTitleImage()
            {
                UIImage logoImage = UIImage.FromFile(ResourceFiles.ImageResources.LogoImageName);
                UIImageView logoImageView = new UIImageView(logoImage);
    
                if (this.NavigationController != null)
                {
                    this.NavigationController.NavigationBar.TopItem.TitleView = logoImageView;
                }
            }
        }
    }
    

    Hope it helps someone!

提交回复
热议问题