コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationRenderer))]
public class CustomNavigationRenderer : NavigationRenderer
{
private const string fontName = "フォントのPostScript名";
public override void ViewDidLoad()
{
base.ViewDidLoad();
//ナビゲーションバーのスタイルを適用する
//iOS13以降はAppDelegateでも実施
CustomNavigationRenderer.SetNavigationStyle(this);
}
/// <summary>
/// ナビゲーションバーのスタイルを適用する
/// </summary>
/// <param name="instance">NavigationRendererのインスタンス</param>
public static void SetNavigationStyle(IVisualElementRenderer instance)
{
//ナビゲーションのスタイル変更
UINavigationBar.Appearance.BarTintColor = UIColor.White;
UINavigationBar.Appearance.TintColor = UIColor.White;
UINavigationBar.Appearance.BackgroundColor = UIColor.Blue;
//タイトル用のフォント
var titleAttr = new UIStringAttributes()
{
Font = UIFont.FromName(fontName, 20),
ForegroundColor = UIColor.White,
BackgroundColor = UIColor.Blue
};
UINavigationBar.Appearance.TitleTextAttributes = titleAttr;
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
//ナビゲーションバーボタン用のフォント
var buttonAttr = new UIStringAttributes()
{
Font = UIFont.FromName(fontName, 16),
ForegroundColor = UIColor.White,
BackgroundColor = UIColor.Blue
};
var appr = new UINavigationBarAppearance();
appr.ConfigureWithTransparentBackground();
appr.TitleTextAttributes = titleAttr;
var dic = new NSDictionary<NSString, NSObject>(
buttonAttr.Dictionary.Keys.Select(r => r as NSString).ToArray(),
buttonAttr.Dictionary.Values
);
var button = new UIBarButtonItemAppearance(UIBarButtonItemStyle.Plain);
button.Normal.TitleTextAttributes = dic;
button.Highlighted.TitleTextAttributes = dic;
appr.ButtonAppearance = button;
appr.BackButtonAppearance = button;
var done = new UIBarButtonItemAppearance(UIBarButtonItemStyle.Done);
done.Normal.TitleTextAttributes = dic;
done.Highlighted.TitleTextAttributes = dic;
appr.DoneButtonAppearance = done;
UINavigationBar.Appearance.StandardAppearance = appr;
UINavigationBar.Appearance.ScrollEdgeAppearance = appr;
UINavigationBar.Appearance.CompactAppearance = appr;
if (instance != null)
{
//戻るボタンがフォント変更されない場合
var bar = instance.NativeView?.Subviews?.Where(r => r.GetType().BaseType == typeof(UIKit.UINavigationBar)).FirstOrDefault();
if (bar != null)
{
((UIKit.UINavigationBar)bar).StandardAppearance = appr;
}
}
}
}
}
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
//ナビゲーションタイトルの色変更
//NavigationRendererに実装している機能を呼び出す
CustomNavigationRenderer.SetNavigationStyle(null);
return base.FinishedLaunching(app, options);
}
}
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(Page), typeof(CustomPageRenderer))]
public class CustomPageRenderer : PageRenderer
{
private const string fontName = "フォントのPostScript名";
public override void ViewWillAppear(bool animated)
{
if (this.NavigationController == null ||
this.NavigationController.TopViewController == null)
{
return;
}
UINavigationItem navigationItem = this.NavigationController.TopViewController.NavigationItem;
if (navigationItem != null)
{
//戻るボタンもフォント変更する場合
navigationItem.BackBarButtonItem.SetTitleTextAttributes(new UITextAttributes()
{
Font = UIFont.FromName(fontName, 16),
//イメージピッカーのキャンセルボタンまで白文字になって見えないので、TextColorは指定しない
//TextColor = UIColor.White,
}, UIControlState.Normal);
}
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。