【发布时间】:2019-10-01 16:13:55
【问题描述】:
我正在使用 Xamarin 编写我的应用程序,但我可以翻译任何 swift/objective-C 响应。
我有一个 WKWebView,用于通过 LoadHtmlString() 显示数据。当用户聚焦输入字段时,我想更改显示在键盘上方的 UIToolbar 的背景颜色。现在,按钮文本颜色和背景颜色都是白色的,所以按钮是不可见的。
我可以使用UIBarButtonItem.Appearance.TintColor = UIColor.White; 更改按钮文本颜色,但这也会更改 UINavigationBar 中按钮的文本颜色。我宁愿将文本保留为白色,并更改 UIToolbar 的背景颜色。
我可以使用UIToolbar.Appearance.BackgroundColor = UIColor.Red; 稍微修改背景颜色,但这只是为 UIToolbar 添加了一种微妙的颜色,实际上并没有使工具栏变得足够暗以使白色文本可见。
这是我设置外观默认值的完整代码:
UIColor lightColor = UIColor.White;
UIColor themeColor = UIColor.Red;
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
UINavigationBar.Appearance.BarStyle = UIBarStyle.Black;
UINavigationBar.Appearance.BarTintColor = themeColor;
UINavigationBar.Appearance.Translucent = false;
UINavigationBar.Appearance.TintColor = lightColor;
UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes()
{
TextColor = lightColor
});
UIStringAttributes navBarAttributes = new UIStringAttributes();
navBarAttributes.ForegroundColor = lightColor;
UINavigationBar.Appearance.TitleTextAttributes = navBarAttributes;
UITabBar.Appearance.BarTintColor = themeColor;
UITabBar.Appearance.TintColor = lightColor;
UIToolbar.Appearance.BarTintColor = themeColor;
UIToolbar.Appearance.TintColor = lightColor;
UIBarButtonItem.Appearance.TintColor = lightColor;
UIButton.AppearanceWhenContainedIn(typeof(UINavigationBar)).TintColor = lightColor;
UIToolbar.Appearance.BackgroundColor = themeColor;
有没有一种方法可以更改 WKWebView 的工具栏背景颜色而无需重新设置我的整个应用程序的主题?
如果我无法更改工具栏的背景颜色,我愿意在 WKWebView 中显示时更改按钮文本颜色。我尝试添加以下代码,但似乎没有任何效果。
//Using green to see if the code is working.
UIBarButtonItem.AppearanceWhenContainedIn(typeof(WKWebView)).TintColor = UIColor.Green;
UIBarButtonItem.AppearanceWhenContainedIn(typeof(MyViewController)).TintColor = UIColor.Green;
【问题讨论】:
标签: ios xamarin.ios