我正在开发一个适用于iPhone和iPad的模块。在这个模块中,我试图模仿UIActivityViewController并显示iPhone和iPad的自定义。
问题:Popover显示在iPad屏幕的左上角。我显示Popup的按钮被创建到Titanium SDK中,我将相同的Button作为参数传递给模块,而模块又将使用相同的按钮在特定位置显示Popover。
请检查我的代码: [代码比粘贴的代码大得多,但我简化了代码并仅粘贴了显示我的代码如何工作的基础知识。]
Titanium Code:
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
});
});
Titanium Module:com.xyz.sharing
module.h中
#import "TiModule.h"
@interface ComXYZSharingModule : TiModule {
}
- (id) shareViewController;
@end
Module.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
Proxy.h
@interface ComXYZSharingProxy : TiProxy
@property (nonatomic, strong) ComXYZSharingModule *module;
@property (nonatomic, strong) NSDictionary *content;
@property (strong, nonatomic) TiViewProxy *exportView;
- (void) shareContent : (id) args;
@end
Proxy.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,那么结果也一样。
答案 0 :(得分:1)
你工作太辛苦了。
[popover presentFromRect:self.exportView.bounds
inView:self.exportView
permittedArrowDirections:arrowDirections
animated:animated];
Ti.AirPrint模块演示了这一点:https://github.com/appcelerator/titanium_modules/blob/master/airprint/mobile/ios/Classes/TiAirprintModule.m#L119
答案 1 :(得分:0)
我刚刚更换了以下代码
[popover presentPopoverFromRect:rect inView:[[TiApp app] controller].view permittedArrowDirections:arrowDirections animated:animated];
与
[popover presentPopoverFromBarButtonItem:[view barButtonItem] permittedArrowDirections:UIPopoverArrowDirectionAny animated: animated];
在shareContent函数中解决了我的问题。