2013-08-14 2 views
1

Я хочу изменить цвет UIPopover. Я создал пользовательский popover, но я не могу понять, почему он не показывает границы.Настройка 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.

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

Я пытаюсь создать поповер как это (я не говорю о содержании поповера)

enter image description here

Я просто хочу, чтобы установить пользовательский цвет границ, фон и стрелку. На самом деле я могу установить цвет, но не показывать границы. Как я могу показать границы?

+0

Что происходит, когда вы возвращаете 'YES' из' хочетDefaultContentAppearance'? Документы говорят что-то о рисовании вставки. Это может быть связано. – Tricertops

+0

Ничего не происходит ... – amone

ответ

0

Да, я нашел решение.

(UIEdgeInsets)contentViewInsets метод устанавливает отступ popper.

+ (UIEdgeInsets)contentViewInsets 
{ 
    return UIEdgeInsetsMake(kBorderInset, 10.0f, 10.0f, 10.0f); 
} 
+0

У вас есть реализованный метод, но константа '#define kBorderInset 0.0f'. – Tricertops

+0

@iMartin да я осознал свою ошибку;) – amone

+0

Я нашел еще одно руководство, объясняющее, что именно я хочу http://blog.teamtreehouse.com/customizing-the-design-of-uipopovercontroller – amone