iOS 上 Xamarin 中所选 TabBarItem 的背景颜色
Background color of selected TabBarItem in Xamarin on iOS
使用 Xamarin.Forms,我在 iOS 中有一个自定义的 TabbedPageRenderer。现在,我可以更改所选 TabBarItem 上的文本颜色,但无法更改所选选项卡的背景颜色。有人知道怎么做吗?
class CustomTabbedPageRenderer : TabbedRenderer
{
public override UIViewController SelectedViewController
{
get
{
UITextAttributes attr = new UITextAttributes();
attr.TextColor = UIColor.White;
if (base.SelectedViewController != null)
{
base.SelectedViewController.TabBarItem.SetTitleTextAttributes(attr, UIControlState.Normal);
// TODO: How to set background color for ONE item?
}
return base.SelectedViewController;
}
set
{
base.SelectedViewController = value;
}
}
}
您可以使用appearance API更改标签页的背景颜色
或
您可以使用自定义渲染(正如您在此处尝试的那样)
[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabbedPageCustom))]
namespace MobileCRM.iOS {
public class TabbedPageCustom : TabbedRenderer {
public TabbedPageCustom () {
TabBar.TintColor = MonoTouch.UIKit.UIColor.Black;
TabBar.BarTintColor = MonoTouch.UIKit.UIColor.Blue;
TabBar.BackgroundColor = MonoTouch.UIKit.UIColor.Green;
}
}
}
希望你能从这里继续...
最佳解决方案:
在TabbedRenderer
方法ViewWillAppear
中设置Appearance
代码:
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace TabbedPageWithNavigationPage.iOS
{
class CustomTabbedPageRenderer : TabbedRenderer
{
public UIImage imageWithColor(CGSize size)
{
CGRect rect = new CGRect(0, 0, size.Width, size.Height);
UIGraphics.BeginImageContext(size);
using (CGContext context = UIGraphics.GetCurrentContext())
{
context.SetFillColor(UIColor.Red.CGColor);
context.FillRect(rect);
}
UIImage image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return image;
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
CGSize size = new CGSize(TabBar.Frame.Width / TabBar.Items.Length, TabBar.Frame.Height);
//Background Color
UITabBar.Appearance.SelectionIndicatorImage = imageWithColor(size);
//Normal title Color
UITabBarItem.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White }, UIControlState.Normal);
//Selected title Color
UITabBarItem.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.Black }, UIControlState.Selected);
}
}
}
使用 Xamarin.Forms,我在 iOS 中有一个自定义的 TabbedPageRenderer。现在,我可以更改所选 TabBarItem 上的文本颜色,但无法更改所选选项卡的背景颜色。有人知道怎么做吗?
class CustomTabbedPageRenderer : TabbedRenderer
{
public override UIViewController SelectedViewController
{
get
{
UITextAttributes attr = new UITextAttributes();
attr.TextColor = UIColor.White;
if (base.SelectedViewController != null)
{
base.SelectedViewController.TabBarItem.SetTitleTextAttributes(attr, UIControlState.Normal);
// TODO: How to set background color for ONE item?
}
return base.SelectedViewController;
}
set
{
base.SelectedViewController = value;
}
}
}
您可以使用appearance API更改标签页的背景颜色
或
您可以使用自定义渲染(正如您在此处尝试的那样)
[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabbedPageCustom))]
namespace MobileCRM.iOS {
public class TabbedPageCustom : TabbedRenderer {
public TabbedPageCustom () {
TabBar.TintColor = MonoTouch.UIKit.UIColor.Black;
TabBar.BarTintColor = MonoTouch.UIKit.UIColor.Blue;
TabBar.BackgroundColor = MonoTouch.UIKit.UIColor.Green;
}
}
}
希望你能从这里继续...
最佳解决方案:
在TabbedRenderer
ViewWillAppear
中设置Appearance
代码:
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace TabbedPageWithNavigationPage.iOS
{
class CustomTabbedPageRenderer : TabbedRenderer
{
public UIImage imageWithColor(CGSize size)
{
CGRect rect = new CGRect(0, 0, size.Width, size.Height);
UIGraphics.BeginImageContext(size);
using (CGContext context = UIGraphics.GetCurrentContext())
{
context.SetFillColor(UIColor.Red.CGColor);
context.FillRect(rect);
}
UIImage image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return image;
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
CGSize size = new CGSize(TabBar.Frame.Width / TabBar.Items.Length, TabBar.Frame.Height);
//Background Color
UITabBar.Appearance.SelectionIndicatorImage = imageWithColor(size);
//Normal title Color
UITabBarItem.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.White }, UIControlState.Normal);
//Selected title Color
UITabBarItem.Appearance.SetTitleTextAttributes(new UITextAttributes { TextColor = UIColor.Black }, UIControlState.Selected);
}
}
}