自定义 UIPopover
Posted
技术标签:
【中文标题】自定义 UIPopover【英文标题】:Customizing UIPopover 【发布时间】:2013-08-14 17:39:32 【问题描述】:我想改变 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);
我正在尝试创建这样的弹出框(我不是在谈论弹出框内容)
我只想为边框、背景和箭头设置自定义颜色。实际上我可以设置颜色但不显示边框。如何显示边框?
【问题讨论】:
当您从wantsDefaultContentAppearance
返回YES
时会发生什么?文档说了一些关于绘制插图的事情。可能是相关的。
【参考方案1】:
是的,我找到了解决方案。
(UIEdgeInsets)contentViewInsets 方法设置弹出框的缩进。
+ (UIEdgeInsets)contentViewInsets
return UIEdgeInsetsMake(kBorderInset, 10.0f, 10.0f, 10.0f);
【讨论】:
你已经实现了方法,但是常量是#define kBorderInset 0.0f
。
@iMartin 是的,我意识到我的错误;)
我发现另一个教程准确地解释了我想要什么blog.teamtreehouse.com/…以上是关于自定义 UIPopover的主要内容,如果未能解决你的问题,请参考以下文章
像 UIPopoverController 中的方式一样关闭自定义 UIView