如何在iOS7中将外观(角点)更改为UIPopoverController

时间:2014-03-27 02:20:52

标签: xcode5 uipopovercontroller ios7.1

我有这个UIPopoverController:

enter image description here

默认情况下,角是圆角的,但是我需要它们是方形的,是否可能?如果是的话......怎么做?

1 个答案:

答案 0 :(得分:1)

终于找到了这样做的方法

http://blog.teamtreehouse.com/customizing-the-design-of-uipopovercontroller

图形:

  • popoverControllerBackground.png:
  • popoverControllerArrow.png:

popoverControllerBackground.png popoverControllerArrow.png

代码:

CustomPopoverBackgroundView.h:

#import <UIKit/UIPopoverBackgroundView.h>

#import "ViewController.h"

@interface CustomPopoverBackgroundView : UIPopoverBackgroundView {
    UIImageView *_borderImageView;
    UIImageView *_arrowView;
    CGFloat _arrowOffset;
    UIPopoverArrowDirection _arrowDirection;
}

@end

CustomPopoverBackgroundView.m

#import "CustomPopoverBackgroundView.h"

#define CONTENT_INSET 10.0
#define CAP_INSET 25.0
#define ARROW_BASE 25.0
#define ARROW_HEIGHT 12.0

@implementation CustomPopoverBackgroundView

-(id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _borderImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"popoverControllerBackground.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(CAP_INSET,CAP_INSET,CAP_INSET,CAP_INSET)]];

        _arrowView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"popoverControllerArrow.png"]];

        [self addSubview:_borderImageView];
        [self addSubview:_arrowView];

    }
    return self;
}

- (CGFloat) arrowOffset {
    return _arrowOffset;
}

- (void) setArrowOffset:(CGFloat)arrowOffset {
    _arrowOffset = arrowOffset;
}

- (UIPopoverArrowDirection)arrowDirection {
    return _arrowDirection;
}

- (void)setArrowDirection:(UIPopoverArrowDirection)arrowDirection {
    _arrowDirection = arrowDirection;
}


+(UIEdgeInsets)contentViewInsets{
    return UIEdgeInsetsMake(CONTENT_INSET, CONTENT_INSET, CONTENT_INSET, CONTENT_INSET);
}

+(CGFloat)arrowHeight{
    return ARROW_HEIGHT;
}

+(CGFloat)arrowBase{
    return ARROW_BASE;
}

-  (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat _height = self.frame.size.height;
    CGFloat _width = self.frame.size.width;
    CGFloat _left = 0.0;
    CGFloat _top = 0.0;
    CGFloat _coordinate = 0.0;
    CGAffineTransform _rotation = CGAffineTransformIdentity;


    switch (self.arrowDirection) {
        case UIPopoverArrowDirectionUp:
            _top += ARROW_HEIGHT;
            _height -= ARROW_HEIGHT;
            _coordinate = ((self.frame.size.width / 2) + self.arrowOffset) - (ARROW_BASE/2);
            _arrowView.frame = CGRectMake(_coordinate, 0, ARROW_BASE, ARROW_HEIGHT);
            break;


        case UIPopoverArrowDirectionDown:
            _height -= ARROW_HEIGHT;
            _coordinate = ((self.frame.size.width / 2) + self.arrowOffset) - (ARROW_BASE/2);
            _arrowView.frame = CGRectMake(_coordinate, _height, ARROW_BASE, ARROW_HEIGHT);
            _rotation = CGAffineTransformMakeRotation( M_PI );
            break;

        case UIPopoverArrowDirectionLeft:
            _left += ARROW_BASE;
            _width -= ARROW_BASE;
            _coordinate = ((self.frame.size.height / 2) + self.arrowOffset) - (ARROW_HEIGHT/2);
            _arrowView.frame = CGRectMake(0, _coordinate, ARROW_BASE, ARROW_HEIGHT);
            _rotation = CGAffineTransformMakeRotation( -M_PI_2 );
            break;

        case UIPopoverArrowDirectionRight:
            _width -= ARROW_BASE;
            _coordinate = ((self.frame.size.height / 2) + self.arrowOffset)- (ARROW_HEIGHT/2);
            _arrowView.frame = CGRectMake(_width, _coordinate, ARROW_BASE, ARROW_HEIGHT);
            _rotation = CGAffineTransformMakeRotation( M_PI_2 );

            break;

    }

    _borderImageView.frame =  CGRectMake(_left, _top, _width, _height);


    [_arrowView setTransform:_rotation];

}

@end

如何调用它:

进行导入:

#import "CustomPopoverBackgroundView.h"

让你的方法来调用它:

-(IBAction)openPopupUserProfile:(id)sender{
    UIStoryboard *storyBoard        = [UIStoryboard storyboardWithName:@"iPad" bundle:nil];

    if ([[UIDevice currentDevice].model hasPrefix:@"iPad"]){
        if (!popover || !popover.popoverVisible)
        {
            MenuPerfilUsuarioVC *controller     = [storyBoard instantiateViewControllerWithIdentifier:@"MenuPerfilUsuarioVC"];
            controller.title = @"";

            popover  = [[UIPopoverController alloc] initWithContentViewController:controller];
            popover.popoverBackgroundViewClass = [CustomPopoverBackgroundView class];


            [popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }
        else{
            [popover dismissPopoverAnimated:YES];
            popover = nil;
        }
    }
}

这是我的结果:

enter image description here