为视图控制器根视图设置外观代理 [英] set appearance proxy for view controller root view

查看:25
本文介绍了为视图控制器根视图设置外观代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在使用 UIAppearance 时仅针对视图控制器的根视图?我想从我的应用程序委托为我的所有控制器设置背景颜色,但只想定位视图控制器上的直接视图.谢谢.

is it possible to target only a view controller's root view when using UIAppearance? I would like to setBackgroundColor for all of my controllers from my app delegate, but want to target only the direct view on a view controller. Thanks.

详细来说,每个 UIViewController 子类都有自己的 UIView 对象,您可以通过其 view 属性调用.这就是你在故事板中放置任何东西的视图,比如 UITableView、UITabBar 等.我希望代理仅适用于该直接视图,因为 UIKit 中的所有内容几乎都是 UIView 的子类.

To elaborate, each UIViewController subclass has its own UIView object you can call via its view property. That's the view you put any stuff on like UITableView, UITabBar, etc in storyboard. I want the proxy to only apply to that direct view, since everything in UIKit is pretty much a subclass of UIView.

这样做的目的是集中控制我的视图控制器外观.我突然想到,也许我应该只使用 UIViewController 的子类,但我想有另一个地方做我刚刚描述的事情仍然很酷.

The purpose for this is is to centralize control on my view controllers appearance. It's occurring to me that perhaps maybe I should just use subclass of UIViewController, but I guess it'd still be cool to have another place to do what I just described.

推荐答案

很遗憾,您无法使用外观代理完成此操作.但是,如果您使用 swift 并从故事板或笔尖加载视图控制器,一种解决方案是扩展 UIViewController 并像这样覆盖awakeFromNib:

Unfortunately, you can't accomplish this using the appearance proxy. However, one solution if you are using swift and loading your view controllers from storyboards or nibs is to extend UIViewController and override awakeFromNib like so:

extension UIViewController {

    open override func awakeFromNib() {
        super.awakeFromNib()

        view.backgroundColor = UIColor.blue
    }
}

这篇关于为视图控制器根视图设置外观代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆