iOS - 防止 UIAppearance 设置更改 UIWebView 输入附件视图

Posted

技术标签:

【中文标题】iOS - 防止 UIAppearance 设置更改 UIWebView 输入附件视图【英文标题】:iOS - Prevent UIAppearance settings from changing UIWebView input accessory view 【发布时间】:2012-06-26 19:51:23 【问题描述】:

我的应用程序中有一个 UIWebView,问题是我有一个 UIAppearance 可以修改分段控件的外观,因此它会修改 UIWebView 文本字段的输入附件视图中的分段控件,我希望它看起来正确,或者不尝试修改它。这是它目前的样子:

【问题讨论】:

我也有同样的问题。这让我发疯了。 这似乎不是 UISegmentedControl 的外观设置,而是 UIToolbar 的外观,我不太确定,但如果不删除我的外观,我似乎无法修复它。似乎也没有简单安全的方法可以简单地从 UIWebView 中删除工具栏 是的,似乎问题在于它正在更改分段控制器未选择区域的 alpha。我想知道这是否与 UIToolbar 本身具有透明度有关。 于是我与当地的 Cocoaheads 分会进行了交谈,得到了一些想法。其中一个人写了这篇关于如何删除附件视图bjhomer.blogspot.com/2012/03/… 的博文,其他人则建议使用appearanceWhenContainedIn:不知何故。 我想过包含在里面的样子,但是包含在什么里面? 【参考方案1】:

我刚刚通过使用 [UIAppearance appearanceWhenContainedIn:] 而不是 [UIAppearance appearance] 为自己解决了这个问题。

通过向选择器发送一个包含您要自定义的元素的所有自定义 ViewController 类的零终止列表来使用它。所以不是

[[UISegmentedControl appearance] setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

使用

[[UISegmentedControl appearanceWhenContainedIn:[MyViewController class], [MyOtherViewController class], nil] setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

您不需要列出所有的 ViewController,只需要列出您的***视图。这也使得以下技巧成为可能:创建 UIViewController 的“空”子类 CustomizedViewController,它除了作为子类之外什么都不做 - 然后在整个代码中从那里继承子类,而不是从 UIViewController(基本上用“CustomizedViewController”替换所有出现的“UIViewController” - 除非 CustomizedViewController 实际上将 UIViewController 声明为其超类。

然后使用

[[UISegmentedControl appearanceWhenContainedIn:[CustomizedViewController class], nil] setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

【讨论】:

我已经失去了对这个答案的信心,我稍后会试一试,Stefan。谢谢。 不幸的是,我正在处理的项目有数百个类跨越多个团队,使用我无法控制的代码。 UIAppearance 的美妙之处在于(理论上)我不必弄乱我试图皮肤的类。我不能通过我想要皮肤的每个类并给它们一个通用的超类来让 UIAppearance 工作。也许如果苹果包含了一个外观WhenNotContainedIn 方法,我至少可以在 UIWebView 中抑制 UIToolbars 的样式... 对我来说它不起作用...当我向视图控制器展示 Web 视图时,工具栏是默认外观,但键盘工具栏是自定义的【参考方案2】:

好的,所以我终于能够找到解决方法。请参阅我对当前答案的评论,该评论解释了为什么该答案对我不起作用。

重申一下,据我所知,创建键盘附件的 ios 代码是显式设置 UIToolbar 背景,因此您不会获得 UIAppearance 背景设置,但会获得 UIBarButtonItem 样式。结果是一个半样式视图,看起来很糟糕。幸运的是,我的设计师在这种特殊情况下使用默认样式很好。

所以,真正的诀窍是弄清楚如何让视图不通过 UIAppearance 设置样式。我这样做的方法是替换它:

id barButtonAppearanceProxy = [UIBarButtonItem appearance];

有了这个:

// wtf: Style any UIBarButtonItem associated with a UIViewController
id barButtonAppearanceProxy = [UIBarButtonItem appearanceWhenContainedIn:[UIViewController class], nil];

起初这对我来说似乎很奇怪,因为我正在考虑根据视图层次结构进行包含...但是,这只是说我们只会设置与 UIViewController 关联的 UIBarButtonItems 的样式。这不会影响键盘附件的原因是因为它直接添加了父视图的窗口,并且没有与 UIViewController 关联。

这仍然不是一个完美的解决方案,因为理想情况下我们只需使用 UIAppearance 设置键盘配件的样式。此外,它假定您的所有工具栏都与 UIViewController 相关联。对我(可能还有你们中的大多数人)来说幸运的是,通常情况就是这样。

【讨论】:

以上是关于iOS - 防止 UIAppearance 设置更改 UIWebView 输入附件视图的主要内容,如果未能解决你的问题,请参考以下文章

如何防止使用 UIAppearance 和 UIView 的 tintColor 更改所有 UITabBar 图标颜色?

iOS 应用的配色方案 - UIView 和 UIAppearance - 设置背景颜色

如何从 UILabel 中删除 UIAppearance 设置?

使用 UIAppearance 代理设置 UILabel 样式

如何使用 UIAppearance 设置 ViewController 的 backgroundColor

我啥时候可以开始使用使用 UIAppearance 设置的属性?