自定义UIPopover

时间:2013-08-14 17:39:32

标签: objective-c ipad uipopover

我想更改UIPopover的颜色。我已经创建了一个自定义弹出框,但我无法弄清楚为什么不显示边框。

enter image description here

这是我的MainViewController文件,显示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;
    }
}

这是从UIPopoverBackgroundView继承的Back类。

#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);
}

我正在尝试创建这样的popover(我不是在讨论popover内容)

enter image description here

我只想为边框,背景和箭头设置自定义颜色。实际上我可以设置颜色但不显示边框。我该如何显示边框?

1 个答案:

答案 0 :(得分:0)

是的,我找到了解决方案。

(UIEdgeInsets)contentViewInsets方法设置popover的缩进。

+ (UIEdgeInsets)contentViewInsets
{
    return UIEdgeInsetsMake(kBorderInset, 10.0f, 10.0f, 10.0f);
}
相关问题