如何从Cordova插件添加本机视图 [英] How can I add a native view from a Cordova plugin
问题描述
我正在开发一个最新版本的Cordova插件(3.3)。我需要在Cordova视图中添加一个本地 UIImageView
。
I am developing a plugin with the latest version of Cordova (3.3). I need to add a native UIImageView
to the Cordova view.
如果我有权访问项目平台文件夹,我可以将我的视图添加到属于 CDVViewController
的视图实例。
If I have access to the project for example in the platform folder, I can add my view to the view instance belonging to CDVViewController
. However, I am not sure how to access that reference from a plugin.
在我的插件里面我有:
@interface CDVCool : CDVPlugin
@property (weak, nonatomic) UIImageView *nativeImageView;
...
@end
如何初始化和呈现此视图通过只修改插件文件?
How can I initialize and render this view by modifying only the plugin files?
推荐答案
这个答案的信用到 devgeeks ,他指出了我的几个插件, MapKit 和 VolumeSlider ,它们以原生元素与cordova网络视图混合。
Credit for this answer goes to devgeeks who pointed me to a couple of his plugins, MapKit and VolumeSlider, that mix in native elements with cordova web view.
关键是覆盖 initWithWebView
方法:
-(CDVPlugin*) initWithWebView:(UIWebView*)theWebView
{
self = (VolumeSlider*)[super initWithWebView:theWebView];
return self;
}
现在在插件里面你可以获得一个对cordova web查看并添加到任何你的心愿望。
Now inside the plugin you can obtain a reference to the view controller behind cordova web view and add to it whatever your heart desire.
[self.webView.superview addSubview:mpCustomView];
这是很酷的,因为你可以控制您添加的任何视图的zPosition相对于webView。因此,您可以将视图置于Web视图的上方或下方。
This is cool becuase you can control the zPosition of any views you add with respect to the webView. So you can put views above or below the web view.
这篇关于如何从Cordova插件添加本机视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!