How can I add a native view from a Cordova plugin

流过昼夜 提交于 2020-01-01 19:21:00

问题


I am developing a plugin with the latest version of Cordova (3.3). I need to add a native UIImageView to the Cordova view.

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.

Inside my plugin I have:

@interface CDVCool : CDVPlugin

@property (weak, nonatomic) UIImageView *nativeImageView;
...
@end

How can I initialize and render this view by modifying only the plugin files?


回答1:


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.

The key is to overwrite the initWithWebView method:

-(CDVPlugin*) initWithWebView:(UIWebView*)theWebView
{
    self = (VolumeSlider*)[super initWithWebView:theWebView];
    return self;
}

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];

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.



来源:https://stackoverflow.com/questions/21767146/how-can-i-add-a-native-view-from-a-cordova-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!