开发者

Apply filter over UIView/UIWindow to modify underlying view rendering

I'm trying to make a "dark mode" for my app, and I'd like to do it in a really easy way. Is there any way to apply a filter or create another view over my other views so that they appear inverted - much like "Accessibility" on iOS 3.2+? I know that Core Image filters don't work on iPhone, but that's not a big deal to me - as long as there's a way开发者_如何学运维 to apply a different sort of filter.

Is this feasible on iPhone? How does Apple do it?


It sounds like you're thinking of placing a semi-transparent view filled with a dark color over your entire user interface. If that's the case, then you should read Forwarding Touch Events for a discussion of the issues involved.

Another way to do it is to change the set of colors and images that you use to draw your views.

Both these schemes will be easier and work better if you're dealing with custom views. If you're using an overlay, the document linked above explains that the standard UI elements don't take kindly to event forwarding. If you change colors, well... depending on what UI elements you're using, it may or may not be possible to substitute your own images and colors.


Try this!

First, create a layer!

let foreground = CALayer() 
foreground.backgroundColor = UIColor.blue.cgColor //Whatever color you want
foreground.opacity = 0.5
foreground.frame = (UIApplication.shared.keyWindow?.bounds)
foreground.masksToBounds = true

Then just add the layer to your root view controller

UIApplication.shared.keyWindow?.rootViewController?.view.layer.addSublayer(foreground)

Hope this can help someone else!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜