0

This is a simple question I ask you: How can I change the background of the QLPreviewController component?

I use it to present PDF files but it shows up with the scrollview pattern as the background color:

[UIColor scrollViewTexturedBackgroundColor]

I would like to change that background color but changing the view's backgroundColor attribute is not helping.

Any ideas?

Hima
  • 1,249
  • 1
  • 14
  • 18
Fabiano Francesconi
  • 1,769
  • 1
  • 19
  • 35

2 Answers2

0

You need to subclass it and make the change. Something like this:

.h file:

#import <QuickLook/QuickLook.h>

@interface MyQLPreviewController : QLPreviewController

.m file:

#import "MyQLPreviewController.h"

@implementation MyQLPreviewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];  // make the change for example
}
user523234
  • 14,323
  • 10
  • 62
  • 102
  • Before actually asking here I tried all these kind of stuff: subclassed, changed in the did load, changed in the did appear.. nothing seems to work. https://skitch.com/elbryan/eb8g3/ios-simulator – Fabiano Francesconi Jun 16 '12 at 14:52
0

I was having a similar issue and ended up subclassing QLPreviewController and adding the following to its implementation:

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    //Find webview and set its subviews' background color to white
    [[self.view subviews] enumerateObjectsUsingBlock:^(UIView* view, NSUInteger idx, BOOL *stop) {

        [self setSubviewsBackgroundColor:view];

    }];
}

and

- (void)setSubviewsBackgroundColor:(UIView*)view{

    [[view subviews] enumerateObjectsUsingBlock:^(UIView* subview, NSUInteger idx, BOOL *stop) {

        if ([subview isKindOfClass:[UIWebView class]]) {

            [subview setBackgroundColor:[UIColor whiteColor]];
            [[subview subviews] enumerateObjectsUsingBlock:^(UIView* view, NSUInteger idx, BOOL *stop) {
                [view setBackgroundColor:[UIColor whiteColor]];
            }];
        }
        else [self setSubviewsBackgroundColor:subview];
    }];
}

Of course you'll likely want to change [UIColor whiteColor] and optimize the above code to suit your needs.

Justin Boo
  • 10,132
  • 8
  • 50
  • 71
  • 2
    This works in iOS 5.1 but does not in iOS 6. I think they are not using an instance of UIWebView anymore. Still, even setting the background color to each instance of UIView's subview won't work. – Fabiano Francesconi Sep 03 '12 at 07:46