Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# iOS上Xamarin中选定TabBarItem的背景色_C#_Xamarin_Xamarin.ios_Xamarin.forms_Custom Renderer - Fatal编程技术网

C# iOS上Xamarin中选定TabBarItem的背景色

C# iOS上Xamarin中选定TabBarItem的背景色,c#,xamarin,xamarin.ios,xamarin.forms,custom-renderer,C#,Xamarin,Xamarin.ios,Xamarin.forms,Custom Renderer,使用Xamarin.Forms,我在iOS中有一个定制的TabbedPageRenderer。现在,我可以更改所选选项卡上的文本颜色,但不能更改所选选项卡的背景颜色。有人知道怎么做吗 class CustomTabbedPageRenderer : TabbedRenderer { public override UIViewController SelectedViewController { get { UITextAttribu

使用Xamarin.Forms,我在iOS中有一个定制的TabbedPageRenderer。现在,我可以更改所选选项卡上的文本颜色,但不能更改所选选项卡的背景颜色。有人知道怎么做吗

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;
       }
    }
}
您可以使用更改选项卡式页面上的背景色

您可以使用自定义渲染(如您在此处尝试的那样)

希望您可以从此处继续…

您可以在选项卡式页面上更改背景颜色

您可以使用自定义渲染(如您在此处尝试的那样)

希望您能从这里继续…

最佳解决方案: 在方法
视图中设置
外观
将在
选项卡渲染器中显示

代码:

最佳解决方案: 在方法
视图中设置
外观
将在
选项卡渲染器中显示

代码:

谢谢您的回答,但我看不到任何关于更改一个项目(不是整行)背景色的内容,但会深入查看外观API.TabBar.BackgroundColor=MonoTouch.UIKit.UIColor.Green;这改变了整个TabBar?我只想更改所选的TabBarItem。谢谢您的回答,但我看不到任何关于更改一个项目(不是整行)背景颜色的内容,但将深入查看外观API.TabBar.BackgroundColor=MonoTouch.UIKit.UIColor.Green;这改变了整个TabBar?我只想更改所选的TabBarItem。效果很好!将删除我的buggy子视图答案并将其设置为已接受答案。谢谢。在iPhoneX问世之前,它看起来还不错。试着去弄清楚,就像一个符咒!将删除我的buggy子视图答案并将其设置为已接受答案。谢谢。在iPhoneX问世之前,它看起来还不错。想弄明白。
[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;         
}    

}

}
[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);
        }
    }
}