需要帮助改善 Xamarin iOS 选项卡的外观
Need help to improve the appearance on Xamarin iOS tab
我想在 ios、
上改进我的内容页面的大小和背景
var About = new ContentPage() { Title = "About" };
var layout = new StackLayout();
var line1 = new Label() { Text = viewModel.Member.Line1, FontSize = 16, HorizontalTextAlignment = TextAlignment.Center };
var MapTab = new ContentPage() {Title = "Map"};
Android:
内容页的标题在 ios 上显得很小,不可见。我需要帮助来尝试改善外观并使其更大。
您必须为您的 TabbedPage
实施 自定义渲染器 。看到这个 link:
Extending TabbedPage in Xamarin Forms.
上面写着:
To do these customizations we will use a custom renderer on each platform to render the TabbedPage.
我将从源代码中复制代码片段作为示例:
YourTabbedPage.xaml:
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CustomTabbedPage.MainPage"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom">
<TabbedPage.Children>
<ContentPage Title="Home" Icon="ic_home.png" BackgroundColor="White"/>
<ContentPage Title="Favorites" Icon="ic_favorite.png" BackgroundColor="White"/>
<ContentPage Title="App" Icon="app_logo_unselected.png" x:Name="home" BackgroundColor="White"/>
<ContentPage Title="Friends" Icon="ic_people.png" BackgroundColor="White"/>
<ContentPage Title="Settings" Icon="ic_settings.png" BackgroundColor="White"/>
</TabbedPage.Children>
</TabbedPage>
iOS 自定义渲染器:
public class ExtendedTabbedPageRenderer : TabbedRenderer
{
public override void ViewWillAppear(bool animated)
{
if (TabBar?.Items == null)
return;
var tabs = Element as TabbedPage;
if (tabs != null)
{
for (int i = 0; i < TabBar.Items.Length; i++)
{
UpdateTabBarItem(TabBar.Items[i], tabs.Children[i].Icon);
}
}
base.ViewWillAppear(animated);
}
private void UpdateTabBarItem(UITabBarItem item, string icon)
{
if (item == null || icon == null)
return;
// Set the font for the title.
item.SetTitleTextAttributes(new UITextAttributes() { Font = UIFont.FromName("GillSans-UltraBold", 12), TextColor = Color.FromHex("#757575").ToUIColor() }, UIControlState.Normal);
item.SetTitleTextAttributes(new UITextAttributes() { Font = UIFont.FromName("GillSans-UltraBold", 12), TextColor = Color.FromHex("#3C9BDF").ToUIColor() }, UIControlState.Selected);
}
}
Android 自定义渲染器:
public class ExtendedTabbedPageRenderer : TabbedPageRenderer
{
Xamarin.Forms.TabbedPage tabbedPage;
BottomNavigationView bottomNavigationView;
Android.Views.IMenuItem lastItemSelected;
int lastItemId=-1;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TabbedPage> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
tabbedPage = e.NewElement as ExtendedTabbedPage;
bottomNavigationView = (GetChildAt(0) as Android.Widget.RelativeLayout).GetChildAt(1) as BottomNavigationView;
bottomNavigationView.NavigationItemSelected += BottomNavigationView_NavigationItemSelected;
//Call to change the font
ChangeFont();
}
if (e.OldElement != null)
{
bottomNavigationView.NavigationItemSelected -= BottomNavigationView_NavigationItemSelected;
}
}
//Change Tab font
void ChangeFont()
{
var fontFace = Typeface.CreateFromAsset(Context.Assets, "gilsansultrabold.ttf");
var bottomNavMenuView = bottomNavigationView.GetChildAt(0) as BottomNavigationMenuView;
for (int i = 0; i < bottomNavMenuView.ChildCount; i++)
{
var item = bottomNavMenuView.GetChildAt(i) as BottomNavigationItemView;
var itemTitle = item.GetChildAt(1);
var smallTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(0));
var largeTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(1));
lastItemId = bottomNavMenuView.SelectedItemId;
smallTextView.SetTypeface(fontFace, TypefaceStyle.Bold);
largeTextView.SetTypeface(fontFace, TypefaceStyle.Bold);
//Set text color
var textColor = (item.Id == bottomNavMenuView.SelectedItemId) ? tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarSelectedItemColor().ToAndroid() : tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarItemColor().ToAndroid();
smallTextView.SetTextColor(textColor);
largeTextView.SetTextColor(textColor);
}
}
void BottomNavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
var normalColor = tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarItemColor().ToAndroid();
var selectedColor = tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarSelectedItemColor().ToAndroid();
if(lastItemId!=-1)
{
SetTabItemTextColor(bottomNavMenuView.GetChildAt(lastItemId) as BottomNavigationItemView, normalColor);
}
SetTabItemTextColor(bottomNavMenuView.GetChildAt(e.Item.ItemId) as BottomNavigationItemView, selectedColor);
this.OnNavigationItemSelected(e.Item);
lastItemId = e.Item.ItemId;
}
void SetTabItemTextColor(BottomNavigationItemView bottomNavigationItemView, Android.Graphics.Color textColor)
{
var itemTitle = bottomNavigationItemView.GetChildAt(1);
var smallTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(0));
var largeTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(1));
smallTextView.SetTextColor(textColor);
largeTextView.SetTextColor(textColor);
}
}
我为我想改进的两个页面创建了一个内容 class,地图和 memberabout 页面,我没有使用内容页面,而是这样做
var About = new MemberAboutPage { Title = "About" };
var layout = new StackLayout();
var MapTab = new MapPage() { Title = "Map" };
然后我将页面添加到我创建的页面并镜像到下面的 ios 渲染页面,此页面格式化选项卡并使它们看起来更漂亮并且也防止 iPhone 上的重叠X. 快乐的编程伙伴
'[assembly: ExportRenderer(typeof(CardPage), typeof(MyiOSTabbedPage))]
[assembly: ExportRenderer(typeof(LoginPage), typeof(MyiOSTabbedPage))]
[assembly: ExportRenderer(typeof(MemberAboutPage), typeof(MyiOSTabbedPage))]
[assembly: ExportRenderer(typeof(MapPage), typeof(MyiOSTabbedPage))]
namespace CHA.iOS.Renderers
{
public class MyiOSTabbedPage : PageRenderer
{
public override void ViewWillLayoutSubviews()
{
base.ViewWillLayoutSubviews();
nfloat tabSize = 44.0f;
UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
CGRect rect = this.View.Frame;
rect.Y = this.NavigationController != null ? tabSize : tabSize + 20;
this.View.Frame = rect;
if (TabBarController != null)
{
CGRect tabFrame = this.TabBarController.TabBar.Frame;
tabFrame.Height = tabSize;
tabFrame.Y = this.NavigationController != null ? 0 : 0;
this.TabBarController.TabBar.Frame = tabFrame;
this.TabBarController.TabBar.BarTintColor = UIColor.FromRGB(234,232,232);
var textAttr = new UITextAttributes
{
Font = UIFont.SystemFontOfSize(20)
};
var selectedAttr = new UITextAttributes
{
TextColor = UIColor.FromRGB(63,165,186),
Font=UIFont.BoldSystemFontOfSize(20)
};
foreach (var i in this.TabBarController.TabBar.Items)
{
i.SetTitleTextAttributes(textAttr, UIControlState.Normal);
i.SetTitleTextAttributes(selectedAttr, UIControlState.Selected);
}
}
}
}'
我想在 ios、
上改进我的内容页面的大小和背景var About = new ContentPage() { Title = "About" };
var layout = new StackLayout();
var line1 = new Label() { Text = viewModel.Member.Line1, FontSize = 16, HorizontalTextAlignment = TextAlignment.Center };
var MapTab = new ContentPage() {Title = "Map"};
Android:
内容页的标题在 ios 上显得很小,不可见。我需要帮助来尝试改善外观并使其更大。
您必须为您的 TabbedPage
实施 自定义渲染器 。看到这个 link:
Extending TabbedPage in Xamarin Forms.
上面写着:
To do these customizations we will use a custom renderer on each platform to render the TabbedPage.
我将从源代码中复制代码片段作为示例:
YourTabbedPage.xaml:
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CustomTabbedPage.MainPage"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom">
<TabbedPage.Children>
<ContentPage Title="Home" Icon="ic_home.png" BackgroundColor="White"/>
<ContentPage Title="Favorites" Icon="ic_favorite.png" BackgroundColor="White"/>
<ContentPage Title="App" Icon="app_logo_unselected.png" x:Name="home" BackgroundColor="White"/>
<ContentPage Title="Friends" Icon="ic_people.png" BackgroundColor="White"/>
<ContentPage Title="Settings" Icon="ic_settings.png" BackgroundColor="White"/>
</TabbedPage.Children>
</TabbedPage>
iOS 自定义渲染器:
public class ExtendedTabbedPageRenderer : TabbedRenderer
{
public override void ViewWillAppear(bool animated)
{
if (TabBar?.Items == null)
return;
var tabs = Element as TabbedPage;
if (tabs != null)
{
for (int i = 0; i < TabBar.Items.Length; i++)
{
UpdateTabBarItem(TabBar.Items[i], tabs.Children[i].Icon);
}
}
base.ViewWillAppear(animated);
}
private void UpdateTabBarItem(UITabBarItem item, string icon)
{
if (item == null || icon == null)
return;
// Set the font for the title.
item.SetTitleTextAttributes(new UITextAttributes() { Font = UIFont.FromName("GillSans-UltraBold", 12), TextColor = Color.FromHex("#757575").ToUIColor() }, UIControlState.Normal);
item.SetTitleTextAttributes(new UITextAttributes() { Font = UIFont.FromName("GillSans-UltraBold", 12), TextColor = Color.FromHex("#3C9BDF").ToUIColor() }, UIControlState.Selected);
}
}
Android 自定义渲染器:
public class ExtendedTabbedPageRenderer : TabbedPageRenderer
{
Xamarin.Forms.TabbedPage tabbedPage;
BottomNavigationView bottomNavigationView;
Android.Views.IMenuItem lastItemSelected;
int lastItemId=-1;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TabbedPage> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
tabbedPage = e.NewElement as ExtendedTabbedPage;
bottomNavigationView = (GetChildAt(0) as Android.Widget.RelativeLayout).GetChildAt(1) as BottomNavigationView;
bottomNavigationView.NavigationItemSelected += BottomNavigationView_NavigationItemSelected;
//Call to change the font
ChangeFont();
}
if (e.OldElement != null)
{
bottomNavigationView.NavigationItemSelected -= BottomNavigationView_NavigationItemSelected;
}
}
//Change Tab font
void ChangeFont()
{
var fontFace = Typeface.CreateFromAsset(Context.Assets, "gilsansultrabold.ttf");
var bottomNavMenuView = bottomNavigationView.GetChildAt(0) as BottomNavigationMenuView;
for (int i = 0; i < bottomNavMenuView.ChildCount; i++)
{
var item = bottomNavMenuView.GetChildAt(i) as BottomNavigationItemView;
var itemTitle = item.GetChildAt(1);
var smallTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(0));
var largeTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(1));
lastItemId = bottomNavMenuView.SelectedItemId;
smallTextView.SetTypeface(fontFace, TypefaceStyle.Bold);
largeTextView.SetTypeface(fontFace, TypefaceStyle.Bold);
//Set text color
var textColor = (item.Id == bottomNavMenuView.SelectedItemId) ? tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarSelectedItemColor().ToAndroid() : tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarItemColor().ToAndroid();
smallTextView.SetTextColor(textColor);
largeTextView.SetTextColor(textColor);
}
}
void BottomNavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
var normalColor = tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarItemColor().ToAndroid();
var selectedColor = tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarSelectedItemColor().ToAndroid();
if(lastItemId!=-1)
{
SetTabItemTextColor(bottomNavMenuView.GetChildAt(lastItemId) as BottomNavigationItemView, normalColor);
}
SetTabItemTextColor(bottomNavMenuView.GetChildAt(e.Item.ItemId) as BottomNavigationItemView, selectedColor);
this.OnNavigationItemSelected(e.Item);
lastItemId = e.Item.ItemId;
}
void SetTabItemTextColor(BottomNavigationItemView bottomNavigationItemView, Android.Graphics.Color textColor)
{
var itemTitle = bottomNavigationItemView.GetChildAt(1);
var smallTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(0));
var largeTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(1));
smallTextView.SetTextColor(textColor);
largeTextView.SetTextColor(textColor);
}
}
我为我想改进的两个页面创建了一个内容 class,地图和 memberabout 页面,我没有使用内容页面,而是这样做
var About = new MemberAboutPage { Title = "About" };
var layout = new StackLayout();
var MapTab = new MapPage() { Title = "Map" };
然后我将页面添加到我创建的页面并镜像到下面的 ios 渲染页面,此页面格式化选项卡并使它们看起来更漂亮并且也防止 iPhone 上的重叠X. 快乐的编程伙伴
'[assembly: ExportRenderer(typeof(CardPage), typeof(MyiOSTabbedPage))]
[assembly: ExportRenderer(typeof(LoginPage), typeof(MyiOSTabbedPage))]
[assembly: ExportRenderer(typeof(MemberAboutPage), typeof(MyiOSTabbedPage))]
[assembly: ExportRenderer(typeof(MapPage), typeof(MyiOSTabbedPage))]
namespace CHA.iOS.Renderers
{
public class MyiOSTabbedPage : PageRenderer
{
public override void ViewWillLayoutSubviews()
{
base.ViewWillLayoutSubviews();
nfloat tabSize = 44.0f;
UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
CGRect rect = this.View.Frame;
rect.Y = this.NavigationController != null ? tabSize : tabSize + 20;
this.View.Frame = rect;
if (TabBarController != null)
{
CGRect tabFrame = this.TabBarController.TabBar.Frame;
tabFrame.Height = tabSize;
tabFrame.Y = this.NavigationController != null ? 0 : 0;
this.TabBarController.TabBar.Frame = tabFrame;
this.TabBarController.TabBar.BarTintColor = UIColor.FromRGB(234,232,232);
var textAttr = new UITextAttributes
{
Font = UIFont.SystemFontOfSize(20)
};
var selectedAttr = new UITextAttributes
{
TextColor = UIColor.FromRGB(63,165,186),
Font=UIFont.BoldSystemFontOfSize(20)
};
foreach (var i in this.TabBarController.TabBar.Items)
{
i.SetTitleTextAttributes(textAttr, UIControlState.Normal);
i.SetTitleTextAttributes(selectedAttr, UIControlState.Selected);
}
}
}
}'