我正在开发一个适用于 iPhone 和 iPad 的模块。在这个模块中,我试图模仿 UIActivityViewController 并为 iPhone 和 iPad 显示自定义。
问题:Popover 显示在 iPad 屏幕的左上角。我从中显示 Popup 的按钮被创建到 Titanium SDK 中,并且我将相同的 Button 作为参数传递给模块,而模块又将使用相同的按钮在特定位置显示 Popover。
请检查下面的代码:[代码比粘贴的代码大得多,但我已经简化了代码,只粘贴了显示我的代码如何工作的基础知识。]
钛代号:
root = {};
var w = Titanium.UI.createWindow({
top : 0,
left : 0,
right : 0,
bottom : 0,
navBarHidden : true,
orientationModes : [Ti.UI.PORTRAIT, Ti.UI.UPSIDE_PORTRAIT, Ti.UI.LANDSCAPE_LEFT, Ti.UI.LANDSCAPE_RIGHT]
});
w.open();
var shareItButton = Titanium.UI.createButton({
top : 10,
right : 40,
width: 90,
height: 30,
backgroundImage : 'images/shareit.png'
});
baseview.add(shareItButton);
// Display Popover when shareItButton is clicked
shareItButton.addEventListener('click', function(e) {
var ShareView = require('com.xyz.sharing');
root.shareController = ShareView.shareViewController; // The proxy controller
root.shareController.shareContent({
message: "Share Message",
url: "http://www.google.com",
title: "Share Title",
file: "File path for iBooks functionality",
shareButton: e.source // e.source is actually a button
});
});
钛模块:com.xyz.sharing
模块.h
#import "TiModule.h"
@interface ComXYZSharingModule : TiModule {
}
- (id) shareViewController;
@end
模块.m
@implementation ComXYZSharingModule
// Ignoring built-in methods, I have pasted only Custom methods
- (id) shareViewController {
ComXYZSharingProxy *proxy = [[ComXYZSharingProxy alloc] init];
proxy.module = self;
return proxy;
}
@end
代理.h
@interface ComXYZSharingProxy : TiProxy
@property (nonatomic, strong) ComXYZSharingModule *module;
@property (nonatomic, strong) NSDictionary *content;
@property (strong, nonatomic) TiViewProxy *exportView;
- (void) shareContent : (id) args;
@end
代理.m
@implementation ComXYZSharingProxy
- (void) shareContent : (id) args {
ENSURE_UI_THREAD(shareContent, args);
NSArray *val = args;
NSDictionary *dict = [val objectAtIndex: 0];
self.content = dict;
if ([dict objectForKey: @"shareButton"]) {
self.exportView = [dict objectForKey: @"shareButton"]; // This is the Button which gives me wrong values
NSLog(@"Button Found", nil);
NSLog([self.exportView description], nil);
CustomActivityController *controller = [[CustomActivityController alloc] init]; // We have just created the controller which is just customization of UIActivityViewController and that's the main view which will be displayed in Popover for iPad and in modal control for iPhone
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:controller];
//[self setPopoverController:popover];
//[popover setDelegate:self];
[popover setBackgroundColor: [UIColor greenColor]];
popover.popoverContentSize = CGSizeMake(320, 250);
CGRect rect = [TiUtils rectValue: [self.exportView rect]];
NSLog([NSString stringWithFormat: @"Size: x: %f,y: %f, width: %f, height: %f", rect.origin.x, rect.origin.y
, rect.size.width, rect.size.height], nil);
[popover presentPopoverFromRect:rect inView:[[TiApp app] controller].view permittedArrowDirections:arrowDirections animated:animated];
}
}
@end
程序运行时输出
Button Found
Size: x: 0.00,y: 0.00, width: 0.00, height: 0.00 [This should be actual size of Button but somehow it's not]
由于这个问题,我的 Popover 显示在 iPad 屏幕的左上角。有人会帮助解决这个问题吗?如果我尝试使用 TiUIButton 或 TiUIButtonProxy 那么结果也是一样的。