【问题标题】:Background color of selected TabBarItem in Xamarin on iOSiOS 上 Xamarin 中所选 TabBarItem 的背景颜色
【发布时间】:2018-04-19 14:40:39
【问题描述】:

使用 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;
       }
    }
}

【问题讨论】:

    标签: c# xamarin xamarin.ios xamarin.forms custom-renderer


    【解决方案1】:

    最佳解决方案:

    在方法ViewWillAppear中设置AppearanceTabbedRenderer

    代码:

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

    【讨论】:

    • 像魅力一样工作!将删除我的错误子视图答案并将其设置为已接受的答案。
    • 谢谢。在 iPhone X 出现之前它看起来不错。试图弄清楚。
    【解决方案2】:

    您可以使用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;         
    }    
    
    }
    
    }
    

    希望你能从这里继续...

    【讨论】:

    • 感谢您的回答,但我看不到任何有关更改一项(不是整行)的背景颜色的信息 - 但会更深入地研究外观 API。
    • TabBar.BackgroundColor = MonoTouch.UIKit.UIColor.Green;
    • 这会改变整个 TabBar?我只想更改选定的 TabBarItem。
    猜你喜欢
    • 2015-07-07
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 2021-08-28
    • 2020-03-26
    • 2020-03-27
    相关资源
    最近更新 更多