2014-03-27 20 views

回答

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