ios - 更改 WKWebView 中 UIToolbar 的背景颜色

标签 ios xamarin.ios

我正在使用 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;

最佳答案

您可以尝试使用自定义 UIToolBar 通过图像更改其背景颜色:

 public class MyToolBar: UIToolbar
{
    public override void Draw(CGRect rect)
    {
        base.Draw(rect);
        CGContext c = UIGraphics.GetCurrentContext();
        UIImage image = new UIImage("background.png");
        //here set image to be background color
        c.DrawImage(rect, image.CGImage);
    }
}

并且工具栏只会在您使用过的地方使用,不会影响应用程序中的其他工具栏。

MyToolBar myToolBar = new MyToolBar();
myToolBar.Frame = new CoreGraphics.CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 50);
UIBarButtonItem doneItem = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done, doneClickMethod);
List<UIBarButtonItem> list = new List<UIBarButtonItem>();
list.Add(doneItem);
myToolBar.Items = list.ToArray();
MySearchBar.InputAccessoryView = myToolBar;

enter image description here

关于ios - 更改 WKWebView 中 UIToolbar 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58188759/

相关文章:

iphone - MKOverlay 有时会消失

iphone - 如何在 Monotouch 中对 UIImageView 进行运动模糊效果?

xamarin.ios - 为了减少内存泄漏,我应该在应用程序中寻找哪些最重要的内容?

c# - iOS:流式传输大文件

php - 使用 PHP 发件人和 Swift 在后台未收到 IOS GCM 推送通知

iphone - 如何在iOS中更改推送通知的消息按钮的文本?

xamarin.ios - 如何在 MonoTouch 中将 NSString 转换为字符串

xamarin.ios - 使用 PCL 构建的 Servicestack monotouch DLL

ios - Quartz2D 或 OpenGL 更适合我的情况

ios - 在 iOS 中将 ImageView 放在另一张图像上