首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法根据灯光模式和DarkMode在iOS中动态设置DarkMode的背景?

有没有办法根据灯光模式和DarkMode在iOS中动态设置DarkMode的背景?
EN

Stack Overflow用户
提问于 2020-01-08 02:12:26
回答 1查看 95关注 0票数 1

我正在尝试在我的应用程序中支持暗模式和亮模式。除了EntryCell之外,它工作得很好。它似乎有它自己的背景,我试着动态设置entrycell背景,但这不起作用。它不与动态资源反应。我尝试为EntryCell动态设置背景颜色也不太走运。现在我只能设置为黑色或白色背景。有什么想法吗?这是我的想法:

这是我的页面渲染器

代码语言:javascript
复制
[assembly: ExportRenderer(typeof(ContentPage), typeof(Mobile.Base.iOS.PageRenderer))]
namespace Mobile.Base.iOS
{

    public class PageRenderer : Xamarin.Forms.Platform.iOS.PageRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || Element == null)
            {
                return;
            }

            try
            {
                SetAppTheme();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"\t\t\tERROR: {ex.Message}");
            }
        }

        public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection)
        {
            base.TraitCollectionDidChange(previousTraitCollection);
            Console.WriteLine($"TraitCollectionDidChange: {TraitCollection.UserInterfaceStyle} != {previousTraitCollection.UserInterfaceStyle}");

            if (previousTraitCollection != null && TraitCollection.UserInterfaceStyle != previousTraitCollection.UserInterfaceStyle)
            {
                SetAppTheme();
            }


        }

        private void SetAppTheme()
        {
            if (TraitCollection.UserInterfaceStyle == UIUserInterfaceStyle.Dark)
            {
                if (App.AppTheme == "dark")
                    return;

                //Add a Check for App Theme since this is called even when not changed really
                App.Current.Resources = new DarkTheme();

                App.AppTheme = "dark";
            }
            else
            {
                if (App.AppTheme != "dark")
                    return;
                App.Current.Resources = new LightTheme();

                App.AppTheme = "light";
            }
        }

    }
}

这是我的自定义条目单元格渲染器

代码语言:javascript
复制
namespace Mobile.Base.iOS
{
    public class customEntryCell : EntryCellRenderer
    {
        public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            var nativeCell = (EntryCell)item;
            var cell = base.GetCell(nativeCell, reusableCell, tv);
            ((UITextField)cell.Subviews[0].Subviews[0]).BackgroundColor = UIColor.White;
            return cell;
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-01-08 10:36:48

您可以通过在自定义渲染器中更改cell.ContentView.BackgroundColor来更改Entrycell's backgroundColor

代码语言:javascript
复制
public class myEntryCelliOSCellRenderer : EntryCellRenderer
{

    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        var nativeCell = (EntryCell)item;

        var cell = base.GetCell(nativeCell, reusableCell, tv);

        ((UITextField)cell.Subviews[0].Subviews[0]).TextColor = UIColor.Orange;
        ((UITextField)cell.Subviews[0].Subviews[0]).BackgroundColor = UIColor.Green;

        //Change the entrycell backgroundColor
        cell.ContentView.BackgroundColor = UIColor.Red;

        return cell;
    }
}

这是EntryCellRenderer的源代码,你可以查看。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59633894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档