【问题标题】:Change background color of UIToolbar in a WKWebView在 WKWebView 中更改 UIToolbar 的背景颜色
【发布时间】: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


    【解决方案1】:

    您可以尝试使用自定义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;
    

    【讨论】:

    • 如何将自定义 UIToolbar 与 WKWebView 一起使用?
    • @Dave 如果在键盘上方添加工具栏,可以参考这个dicussion。
    • 也许我没有正确解释这一点。我没有创建任何 UIToolbar。当用户选择网页中的可编辑字段(例如 标签)时,键盘上方的工具栏会自动显示。该栏包含一个“完成”按钮,以及用于前进到表单中的下一个字段的箭头按钮。我想改变那个工具栏的背景颜色。
    • @Dave Okey,你的意思是网页的键盘焦点?能不能给个截图,我看看。
    • 我最终以不同的方式编写了我的应用程序来解决此问题,因此我无法轻易提供屏幕截图。这个 Stackoverflow 用户也有类似的问题。他还求助于更改他的应用程序,因为您找不到问题的解决方案。 stackoverflow.com/questions/51837022/…
    猜你喜欢
    • 1970-01-01
    • 2021-09-02
    • 2013-08-08
    • 2016-12-30
    • 1970-01-01
    • 2018-03-04
    相关资源
    最近更新 更多