xcode 自定义 QLPreviewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6386492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Customizing QLPreviewController
提问by Mustapha-Tarek
I have an issue in customizing the appearance of my QLPreviewController
.
我在自定义我的QLPreviewController
.
We can display a QLPreviewController by pushing it in a navigation controller, or presenting it in a ModalViewController. Since my navigationController's bar is customized a little (tintColor), I'm pushing the QLPreviewController to preserve my color scheme. But when I push it, the QLPreviewController seems to have some problems : I need to systematically call [qlpvc reloadData]so that my file is displayed.
我们可以通过将 QLPreviewController 推送到导航控制器中来显示它,或者在 ModalViewController 中呈现它。由于我的 navigationController 的栏被自定义了一点 (tintColor),我正在推动 QLPreviewController 以保留我的配色方案。但是当我推送它时,QLPreviewController 似乎有一些问题:我需要系统地调用[qlpvc reloadData]以便显示我的文件。
In iOS [REDACTED], even with reloadData, nothing displays in the pushing way, (actually it displays but in a random way). So I decided it could be interesting to only use the reliable Modal way.
在 iOS [已编辑] 中,即使使用 reloadData,也不会以推送方式显示任何内容(实际上它以随机方式显示)。所以我决定只使用可靠的 Modal 方式会很有趣。
Soooo my point is that I want to present my QLPreviewController in a ModalViewController. It works great that way, but I can't customize the viewController appearance.
Soooo 我的观点是我想在 ModalViewController 中展示我的 QLPreviewController。这样效果很好,但我无法自定义 viewController 外观。
For example in a didSelectRowAtIndexPath
if I do :
例如,didSelectRowAtIndexPath
如果我这样做:
(I don't have my sources near to me so excuse me if I do a mistake)
(我没有我的消息来源,所以如果我做错了,请原谅)
QLPreviewController *qlpvc = [[QLPreviewController alloc] init];
qlpvc.dataSource = self; // Data Source Protocol & methods implemented of course
No need for delegate in my case so //qlpvc.delegate = self;
qlpvc.currentPreviewItemIndex = [indexPath.row];
// The following doesn't work :
[qlpvc.navigationController.navigationBar setTintColor:[UIColor redColor]];
// The following doesn't work too :
[qlpvc.modalViewController.navigationController.navigationBar setTintColor:[UIColor redColor]];
[self presentModalViewController:qlpvc animated:YES];
[qlpvc release];
tl ; dr version :How to manage to customize my modalQLPreviewController's appearance ? Especially the tintColor of the navigationBar ?
; 博士版本:如何管理自定义我的模态QLPreviewController 的外观?尤其是 navigationBar 的 tintColor ?
Thanks a lot.
非常感谢。
回答by Alan Zeino
Subclass QLPreviewController and change the tintColor, et al in viewDidLoad:
.
子类 QLPreviewController 并更改 tintColor 等viewDidLoad:
。
回答by Rob Phillips
This works, but I don't know if it will be rejected by Apple as it's not a published method and may break in future versions of the OS. Works in iOS6.
这有效,但我不知道它是否会被 Apple 拒绝,因为它不是已发布的方法,并且可能会在操作系统的未来版本中失效。适用于iOS6。
Add to the preview controller datasource method:
添加到预览控制器数据源方法:
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{
for (id object in controller.childViewControllers)
{
if ([object isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = object;
navController.navigationBar.tintColor = [UIColor colorWithRed:0.107 green:0.360 blue:0.668 alpha:1.000];
}
}
NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"MyPDFFile" ofType:@"pdf"];
return [NSURL fileURLWithPath:pathToPdfDoc];
}
回答by Brody Robertson
If you are trying to maintain simple styling such as tintColor throughout your app, you should consider using UIAppearance selectors on many UIView classes. The following example customizes all instances of UINavigationBar, including those displayed in QLPreviewController:
如果您试图在整个应用程序中保持简单的样式,例如 tintColor,您应该考虑在许多 UIView 类上使用 UIAppearance 选择器。以下示例自定义 UINavigationBar 的所有实例,包括 QLPreviewController 中显示的实例:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//..
[self initAppearance];
return YES;
}
-(void)initAppearance{
UINavigationBar* defaultNavigationBar = [UINavigationBar appearance];
UIImage *backgroundImage = [UIImage imageNamed:@"MY_IMAGE.png"]
NSDictionary *defaultNavigationBarDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Futura-Medium" size:19], NSFontAttributeName,
[UIColor blueColor], UITextAttributeTextColor,
[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0.0f, 2.0f)], UITextAttributeTextShadowOffset,
nil];
defaultNavigationBar.titleTextAttributes = defaultNavigationBarDictionary; //iOS5
//[defaultNavigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault]; //iOS5
[defaultNavigationBar setBarTintColor:[UIColor redColor]]; //iOS7
[defaultNavigationBar setShadowImage:[[UIImage alloc] init]]; //iOS6, removes shadow
[defaultNavigationBar setTitleVerticalPositionAdjustment:0.0f forBarMetrics:UIBarMetricsDefault]; //iOS5
[defaultNavigationBar setBackIndicatorImage:[UIImage imageNamed:@"BACK_ARROW.png"]]; //iOS7
[defaultNavigationBar setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"BACK_ARROW.png"]]; //iOS7
}