1

I want to change the color of UIPopover. I've created a custom popover but I can't figure out why doesnt show borders.

enter image description here

This is my MainViewController file which shows UIPopover:

@synthesize btnShowPopover;
UIPopoverController *popover;
bool isPopoverOpen = false;

- (void)viewDidLoad
{
    [super viewDidLoad];

    controller = [[Pop1 alloc] initWithNibName:@"Pop1" bundle:nil];
    popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
}

- (IBAction)showPopover:(UIButton *)sender
{
    if(!isPopoverOpen){


        Pop1 *firstViewCtrl = [[Pop1 alloc] init];

        UINavigationController *navbar = [[UINavigationController alloc] initWithRootViewController:firstViewCtrl];

        navbar.contentSizeForViewInPopover = CGSizeMake(300, 300);
        popover = [[UIPopoverController alloc] initWithContentViewController:navbar];

        popover.popoverBackgroundViewClass = [Back class];

        popover.delegate = self;
        popover.popoverContentSize = CGSizeMake(300, 300);

        CGRect popRect = CGRectMake(self.btnShowPopover.frame.origin.x,
                                    self.btnShowPopover.frame.origin.y,
                                    self.btnShowPopover.frame.size.width,
                                    self.btnShowPopover.frame.size.height);
        [popover presentPopoverFromRect:popRect
                                 inView:self.view
               permittedArrowDirections:UIPopoverArrowDirectionAny
                               animated:YES];

        isPopoverOpen = true;
    }else{
        [popover dismissPopoverAnimated:YES];
        isPopoverOpen = false;
    }
}

And this is the Back class inherited from UIPopoverBackgroundView.

#import "Back.h"
#define kArrowBase 30.0f
#define kArrowHeight 20.0f
#define kBorderInset 8.0f
#define kBorderInset 0.0f

@interface Back ()
@property (nonatomic, strong) UIImageView *arrowImageView;
- (UIImage *)drawArrowImage:(CGSize)size;
@end

@implementation Back

@synthesize arrowDirection  = _arrowDirection;
@synthesize arrowOffset     = _arrowOffset;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor grayColor];

        UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.arrowImageView = arrowImageView;
        [self addSubview:self.arrowImageView];
        // Initialization code
    }
    return self;
}

+ (CGFloat)arrowBase
{
    return kArrowBase;
}
+ (CGFloat)arrowHeight
{
    return kArrowHeight;
}
+ (UIEdgeInsets)contentViewInsets
{
    return UIEdgeInsetsMake(kBorderInset, kBorderInset, kBorderInset,       kBorderInset);
}

+ (BOOL)wantsDefaultContentAppearance
{
    return NO;
}

- (UIImage *)drawArrowImage:(CGSize)size
{
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor clearColor] setFill];
    CGContextFillRect(ctx, CGRectMake(0.0f, 0.0f, size.width, size.height));
    CGMutablePathRef arrowPath = CGPathCreateMutable();
    CGPathMoveToPoint(arrowPath, NULL, (size.width/2.0f), 0.0f);
    CGPathAddLineToPoint(arrowPath, NULL, size.width, size.height);
    CGPathAddLineToPoint(arrowPath, NULL, 0.0f, size.height);
    CGPathCloseSubpath(arrowPath);
    CGContextAddPath(ctx, arrowPath);
    CGPathRelease(arrowPath);
    UIColor *fillColor = [UIColor yellowColor];
    CGContextSetFillColorWithColor(ctx, fillColor.CGColor);
    CGContextDrawPath(ctx, kCGPathFill);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGSize arrowSize = CGSizeMake([[self class] arrowBase], [[self class] arrowHeight]);
    self.arrowImageView.image = [self drawArrowImage:arrowSize];
    self.arrowImageView.frame = CGRectMake(((self.bounds.size.width - arrowSize.width) ,kBorderInset), 0.0f, arrowSize.width, arrowSize.height);
}

I am trying to create a popover like this (I am not talking about popover content)

enter image description here

I just want to set a custom color for borders, background and arrow. Actually I can set a color but doesn't show borders. How can I show borders?

amone
  • 3,712
  • 10
  • 36
  • 53
  • What happens when you return `YES` from `wantsDefaultContentAppearance`? Docs say something about drawing insets. It may be related. – Tricertops Aug 14 '13 at 18:01

1 Answers1

0

Yes, I found the solution.

(UIEdgeInsets)contentViewInsets method sets the indent of the popover.

+ (UIEdgeInsets)contentViewInsets
{
    return UIEdgeInsetsMake(kBorderInset, 10.0f, 10.0f, 10.0f);
}
amone
  • 3,712
  • 10
  • 36
  • 53