33

I'm working on an iPad application and I'm using UIPopoverControllers. I'm at the part where the app needs to be branded and styled and i'm wondering how to change the color / tint of the UIPopoverController? Standard is dark blue but it needs to be another color..

is this possible?

Greets, Thomas

Madhup Singh Yadav
  • 8,110
  • 7
  • 51
  • 84
Thomas Joos
  • 2,451
  • 5
  • 26
  • 30

10 Answers10

52

This is possible starting in iOS 5.0 by subclassing the abstract class UIPopoverBackgroundView and assigning your subclass to the popoverBackgroundViewClass property on your UIPopoverController instance. Unfortunately there is no tintColor property as the popover needs to use images for it's arrow and border in order to achieve smooth animations during dynamic resizing. You can learn more about how to customize the appearance of a UIPopoverController in the UIPopoverBackgroundView Class Reference

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
Andrew
  • 7,630
  • 3
  • 42
  • 51
21

It's impossible for now.

It's what I call the "Box in a Box" model. You get control of the box inside of the box (the UIViewController inside of the UIPopoverController), but you have very limited control over the actual popover itself. Outside of the arrow direction and the size, you can't change much else. There are also options for a modal effect popover, which dims everything else when it shows up, but I haven't tried to get it working.

I'm sure you've noticed there is no UIPopover class by now.

The answer you want to hear:
If you really want to style one that bad, just write your own. It's really not that hard.

The link you want to click:
Cocoacontrols is an index of iOS and OSX components available on GitHub, they have some popover stuff.

Sneakyness
  • 5,305
  • 4
  • 33
  • 39
  • 7
    As of iOS5, you can now just subclass UIPopoverBackgroundView and return your subclass to popoverBackgroundViewClass. – steipete Mar 14 '12 at 22:48
13

iOS 7 introduces backgroundColor property of UIPopoverController which affects/includes the navigation background color as well as arrows of popover.

@property (nonatomic, copy) UIColor *backgroundColor NS_AVAILABLE_IOS(7_0);

Usage example:

    if ([self.popoverVC respondsToSelector:@selector(setBackgroundColor:)]) {   // Check to avoid app crash prior to iOS 7
        self.popoverVC.backgroundColor = [UIColor greenColor];   // [UIColor colorWithPatternImage:@"..."] doesn't reflect the color on simulator but on device it works!
    }

Note - As of now (iOS 7.0.3), in some cases (like set color using colorWithPatternImage:), the simulator (and even some devices) doesn't honor the color.

Ashok
  • 6,224
  • 2
  • 37
  • 55
  • And this includes using the alpha channel to have transparent or partially transparent popovers, e.g. popoverController.backgroundColor = UIColor(white: 1, alpha: 0.5) – Ali Beadle Feb 04 '17 at 15:13
  • Don't forget to change the colour/alpha of the view within your popover and controls within that view as appropriate as well. – Ali Beadle Feb 04 '17 at 15:14
8

Throwing my hat in here;

I've leveraged UIPopoverBackgroundViews in iOS 5+ to add a simple tintColor property onto UIPopoverControllers.

PCPopoverController: https://github.com/pcperini/PCPopoverController

Patrick Perini
  • 22,555
  • 12
  • 59
  • 88
  • 1
    I don't believe there's any up, but it couldn't be easier. just use `-initWithContentViewController:andTintColor:` instead of `-initWithContentViewController:`. The rest is the same as `UIPopoverController`. – Patrick Perini May 30 '12 at 13:00
  • +1 nice stuff for popover controller for custom colour. Pretty useful. – Janak Nirmal Sep 27 '12 at 11:34
0

check out these latest projects leveraging UIPopoverBackgroundView https://github.com/CRedit360/C360PopoverBackgroundView https://github.com/GiK/GIKPopoverBackgroundView

johndpope
  • 5,035
  • 2
  • 41
  • 43
0

from ios 5 onward it is can be done, here is a library

https://github.com/ddebin/DDPopoverBackgroundView

just look at the documentation , and it is quite easy

good luck

Kshitiz Ghimire
  • 1,716
  • 3
  • 18
  • 37
0

You can use Elegant Popover cocoapod for just that. You can customise shape and colour of the arrow and the popover itself. Also, you can add colourful borders to the popover.

0

I try to trick it by customizing the view controller inside the popover and then hiding the popover border using this code:

UIView * border = [[insideViewController.view.superview.superview.superview subviews] objectAtIndex:0];  
border.hidden = YES;

The app is actually still in development so I'm hoping other people will comment on this solution.

vangoz
  • 546
  • 6
  • 15
  • 8
    Very fragile. If Apple decides to change something on the popover view hierarchy this code will fall apart. – DarkDust May 09 '12 at 11:44
-1

I know this is a lousy constructed answer, but I've just been playing with the UIPopoverController's views. They do exist.

The only way to access them is from your view that is sitting in the UIPopovercontroller.

I have a navigation controller so I follow this hierarchy

UIView *test = ((UIView *)[[[self.navigationController.view.superview.superview.subviews objectAtIndex:0] subviews] objectAtIndex:1]);
UIView *test2 = ((UIView *)[[[self.navigationController.view.superview.superview.subviews objectAtIndex:0] subviews] objectAtIndex:1]);
test.backgroundColor = [UIColor greenColor];
test2.backgroundColor = [UIColor greenColor];

This isn't exactly the end goal, but it is really close.

you'll find that the_view_in_the_popover.superview.superview (maybe just one superview if you are not reaching out from a navigation controller view) is a UIPopoverView. If you cast it as a UIView and treat it as a UIView you're not really breaking any rules. I guess that is really up to apple though.

maxpower
  • 1,203
  • 11
  • 20
  • that won't work too well - it'll just overlay a colored square behind the popover view. – Pripyat Dec 28 '10 at 13:51
  • 2
    You're right. I think my goal with my answer was just to help people dive into the hierarchy, I don't really have time right now to give a full solution. – maxpower Jan 03 '11 at 17:47
-3

Remove UIPopoverController border:

 NSArray* subviews = ((UIView*)[popupController.contentViewController.view.superview.superview.superview.subviews objectAtIndex:0]).subviews;
for(UIView *subview in subviews){
    [subview removeFromSuperview];
}
ndsantos
  • 13
  • 2