1

我想改变 UIPopover 的颜色。我创建了一个自定义弹出框,但我不知道为什么不显示边框。

在此处输入图像描述

这是显示 UIPopover 的 MainViewController 文件:

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

我正在尝试创建这样的弹出框(我不是在谈论弹出框内容)

在此处输入图像描述

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

4

1 回答 1

0

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

(UIEdgeInsets)contentViewInsets方法设置弹出框的缩进。

+ (UIEdgeInsets)contentViewInsets
{
    return UIEdgeInsetsMake(kBorderInset, 10.0f, 10.0f, 10.0f);
}
于 2013-08-14T19:45:07.697 回答