如何在 iOS7 中将外观(角)更改为 UIPopoverController
Posted
技术标签:
【中文标题】如何在 iOS7 中将外观(角)更改为 UIPopoverController【英文标题】:how to change appearance (corners) to UIPopoverController in iOS7 【发布时间】:2014-03-27 02:20:52 【问题描述】:我有这个 UIPopoverController:
默认情况下,角落是圆的,但我需要它们是方形的,这可能吗???如果是这样...该怎么做?
【问题讨论】:
【参考方案1】:终于找到了办法
http://blog.teamtreehouse.com/customizing-the-design-of-uipopovercontroller
图形:
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;
这是我的结果:
【讨论】:
以上是关于如何在 iOS7 中将外观(角)更改为 UIPopoverController的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iOS7 中将 UIViewController 方向更改为 UIInterfaceOrientationLandscapeLeft 以获取一个视图