iOS下拉列表框 FangPopoverPresentationMenu

PopoverBackgroundView

这是个背景框的样式,链接:https://github.com/ncrabb/iFhMedic3/blob/645e801631313ee2ce30a772ebbaff0d74514626/iFhMedic/DDPopoverBackgroundView.m
为了适应项目,稍微作了修改

//  FangPopoverBackgroundView.h
//  Created by Vicktor on 2017/7/10.
//  Copyright © 2017年 Vicktor. All rights reserved.

#import <UIKit/UIKit.h>

@interface FangPopoverBackgroundView : UIPopoverBackgroundView
{
    CGFloat                     arrowOffset;
    UIPopoverArrowDirection     arrowDirection;
    UIImageView                 *arrowImageView;
    UIImageView                 *popoverBackgroundImageView;
}

@property (nonatomic, readwrite) CGFloat arrowOffset;
@property (nonatomic, readwrite) UIPopoverArrowDirection arrowDirection;

/**
 Adjust content inset (~ border width)
 
 @param contentInset The content inset
 */
+ (void)setContentInset:(CGFloat)contentInset;

/**
 Set tint color used for arrow and popover background
 
 @param tintColor A `UIColor` for tint
 */
+ (void)setTintColor:(UIColor *)tintColor;

/**
 Set arrow width (base)
 
 @param arrowBase Arrow width
 */
+ (void)setArrowBase:(CGFloat)arrowBase;

/**
 Set arrow height
 
 @param arrowHeight Arrow height
 */
+ (void)setArrowHeight:(CGFloat)arrowHeight;

/**
 Set the background image corners radius
 
 @param cornerRadius Border corner radius
 */
+ (void)setBackgroundImageCornerRadius:(CGFloat)cornerRadius;

/**
 Set custom images for background and top/right/bottom/left arrows
 
 @param background Image for background
 @param top Image for top
 @param right Image for right
 @param bottom Image for bottom
 @param left Image for left
 */
+ (void)setBackgroundImage:(UIImage *)background top:(UIImage *)top right:(UIImage *)right bottom:(UIImage *)bottom left:(UIImage *)left;

/**
 Rebuild pre-rendered arrow/background images
 */
+ (void)rebuildArrowImages;
@end

//
//  FangPopoverBackgroundView.m
//  TESTBNR
//
//  Created by Vicktor on 2017/7/10.
//  Copyright © 2017年 Vicktor. All rights reserved.
//

#import "FangPopoverBackgroundView.h"
#import <QuartzCore/QuartzCore.h>

static CGFloat Fang_ArrowBase = 12.0f;
static CGFloat Fang_ArrowHeight = 7.0f;

#define BKG_IMAGE_SIZE 40.0f
#define BKG_IMAGE_CORNER_RADIUS 2 //

static CGFloat Fang_BackgroundImageCornerRadius = BKG_IMAGE_CORNER_RADIUS;
#define BKG_IMAGE_CAPINSET (Fang_BackgroundImageCornerRadius * 2.0f)

static CGFloat Fang_ContentInset = 2.0f;
static UIColor *Fang_TintColor = nil;
static UIImage *Fang_DefaultTopArrowImage = nil;
static UIImage *Fang_DefaultLeftArrowImage = nil;
static UIImage *Fang_DefaultRightArrowImage = nil;
static UIImage *Fang_DefaultBottomArrowImage = nil;
static UIImage *Fang_DefaultBackgroundImage = nil;

@implementation FangPopoverBackgroundView


@synthesize arrowOffset, arrowDirection;


#pragma mark - Overriden class methods

/**
 Return the width of the arrow triangle at its base.
 */
+ (CGFloat)arrowBase
{
    return Fang_ArrowBase;
}

/**
 Return the height of the arrow (measured in points) from its base to its tip.
 */
+ (CGFloat)arrowHeight
{
    return Fang_ArrowHeight;
}

/**
 Return the insets for the content portion of the popover.
 */
+ (UIEdgeInsets)contentViewInsets
{
    return UIEdgeInsetsMake(2.0f, 2.0f, 2.0f, 2.0f);
}


#pragma mark - Custom setters for updating layout

/*
 Whenever arrow changes direction or position, layout subviews will be called
 in order to update arrow and background frames
 */

- (void)setArrowOffset:(CGFloat)_arrowOffset
{
    arrowOffset = _arrowOffset;
    [self setNeedsLayout];
}

- (void)setArrowDirection:(UIPopoverArrowDirection)_arrowDirection
{
    arrowDirection = _arrowDirection;
    [self setNeedsLayout];
}


#pragma mark - Global statics setters

+ (void)setBackgroundImageCornerRadius:(CGFloat)cornerRadius
{
    Fang_BackgroundImageCornerRadius = cornerRadius;
}

+ (void)setContentInset:(CGFloat)contentInset
{
    Fang_ContentInset = contentInset;
}

+ (void)setTintColor:(UIColor *)tintColor
{
    Fang_TintColor = tintColor;
}

+ (void)setArrowBase:(CGFloat)arrowBase
{
    Fang_ArrowBase = arrowBase;
}

+ (void)setArrowHeight:(CGFloat)arrowHeight
{
    Fang_ArrowHeight = arrowHeight;
}

+ (void)setBackgroundImage:(UIImage *)background top:(UIImage *)top right:(UIImage *)right bottom:(UIImage *)bottom left:(UIImage *)left
{
    Fang_DefaultBackgroundImage = background;
    
    Fang_DefaultTopArrowImage = top;
    
    Fang_DefaultRightArrowImage = right;
    
    Fang_DefaultBottomArrowImage = bottom;
    
    Fang_DefaultLeftArrowImage = left;
}


#pragma mark - Initialization

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        if ((Fang_DefaultBackgroundImage == nil) || (Fang_DefaultTopArrowImage == nil) || (Fang_DefaultRightArrowImage == nil) || (Fang_DefaultBottomArrowImage == nil) || (Fang_DefaultLeftArrowImage == nil))
        {
            if (Fang_TintColor == nil) Fang_TintColor = [UIColor blackColor];
            [FangPopoverBackgroundView buildArrowImagesWithTintColor:Fang_TintColor];
        }
        
        popoverBackgroundImageView = [[UIImageView alloc] initWithImage:Fang_DefaultBackgroundImage];
        [self addSubview:popoverBackgroundImageView];
        
        arrowImageView = [[UIImageView alloc] init];
        [self addSubview:arrowImageView];
    }
    
    return self;
}

- (void)dealloc
{
    
}


+ (void)rebuildArrowImages
{
    [FangPopoverBackgroundView buildArrowImagesWithTintColor:Fang_TintColor];
}

+ (void)buildArrowImagesWithTintColor:(UIColor *)tintColor
{
    UIBezierPath *arrowPath;
    
    // top arrow
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(Fang_ArrowBase, Fang_ArrowHeight), NO, 0.0f);
    
    arrowPath = [UIBezierPath bezierPath];
    [arrowPath moveToPoint:   CGPointMake(Fang_ArrowBase/2.0f, 0.0f)];
    [arrowPath addLineToPoint:CGPointMake(Fang_ArrowBase, Fang_ArrowHeight)];
    [arrowPath addLineToPoint:CGPointMake(0.0f, Fang_ArrowHeight)];
    [arrowPath addLineToPoint:CGPointMake(Fang_ArrowBase/2.0f, 0.0f)];
    
    [tintColor setFill];
    [arrowPath fill];
    
    Fang_DefaultTopArrowImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    // bottom arrow
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(Fang_ArrowBase, Fang_ArrowHeight), NO, 0.0f);
    
    arrowPath = [UIBezierPath bezierPath];
    [arrowPath moveToPoint:   CGPointMake(0.0f, 0.0f)];
    [arrowPath addLineToPoint:CGPointMake(Fang_ArrowBase, 0.0f)];
    [arrowPath addLineToPoint:CGPointMake(Fang_ArrowBase/2.0f, Fang_ArrowHeight)];
    [arrowPath addLineToPoint:CGPointMake(0.0f, 0.0f)];
    
    [tintColor setFill];
    [arrowPath fill];
    
    Fang_DefaultBottomArrowImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    // left arrow
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(Fang_ArrowHeight, Fang_ArrowBase), NO, 0.0f);
    
    arrowPath = [UIBezierPath bezierPath];
    [arrowPath moveToPoint:   CGPointMake(Fang_ArrowHeight, 0.0f)];
    [arrowPath addLineToPoint:CGPointMake(Fang_ArrowHeight, Fang_ArrowBase)];
    [arrowPath addLineToPoint:CGPointMake(0.0f, Fang_ArrowBase/2.0f)];
    [arrowPath addLineToPoint:CGPointMake(Fang_ArrowHeight, 0.0f)];
    
    [tintColor setFill];
    [arrowPath fill];
    
    Fang_DefaultLeftArrowImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    // right arrow
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(Fang_ArrowHeight, Fang_ArrowBase), NO, 0.0f);
    
    arrowPath = [UIBezierPath bezierPath];
    [arrowPath moveToPoint:   CGPointMake(0.0f, 0.0f)];
    [arrowPath addLineToPoint:CGPointMake(Fang_ArrowHeight, Fang_ArrowBase/2.0f)];
    [arrowPath addLineToPoint:CGPointMake(0.0f, Fang_ArrowBase)];
    [arrowPath addLineToPoint:CGPointMake(0.0f, 0.0f)];
    
    [tintColor setFill];
    [arrowPath fill];
    
    Fang_DefaultRightArrowImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    // background
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(BKG_IMAGE_SIZE, BKG_IMAGE_SIZE), NO, 0.0f);
    
    UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0f, 0.0f, BKG_IMAGE_SIZE, BKG_IMAGE_SIZE)
                                                          cornerRadius:Fang_BackgroundImageCornerRadius];
    [tintColor setFill];
    [borderPath fill];
    
    UIEdgeInsets capInsets = UIEdgeInsetsMake(BKG_IMAGE_CAPINSET, BKG_IMAGE_CAPINSET, BKG_IMAGE_CAPINSET, BKG_IMAGE_CAPINSET);
    
    Fang_DefaultBackgroundImage = [UIGraphicsGetImageFromCurrentImageContext() resizableImageWithCapInsets:capInsets];
    
    UIGraphicsEndImageContext();
}


#pragma mark - Layout subviews

- (void)layoutSubviews
{
    CGFloat popoverImageOriginX = 0.0f;
    CGFloat popoverImageOriginY = 0.0f;
    
    CGFloat popoverImageWidth = self.bounds.size.width;
    CGFloat popoverImageHeight = self.bounds.size.height;
    
    CGFloat arrowImageOriginX = 0.0f;
    CGFloat arrowImageOriginY = 0.0f;
    
    CGFloat arrowImageWidth = Fang_ArrowBase;
    CGFloat arrowImageHeight = Fang_ArrowHeight;
    
    switch (self.arrowDirection)
    {
        case UIPopoverArrowDirectionUp:
            
            popoverImageOriginY = Fang_ArrowHeight;
            popoverImageHeight = self.bounds.size.height - Fang_ArrowHeight;
            
            // Calculating arrow x position using arrow offset, arrow width and popover width
            arrowImageOriginX = roundf((self.bounds.size.width - Fang_ArrowBase) / 2.0f + self.arrowOffset);
            
            // If arrow image exceeds rounded corner arrow image x postion is adjusted
            if ((arrowImageOriginX + Fang_ArrowBase) > (self.bounds.size.width - Fang_BackgroundImageCornerRadius))
            {
                arrowImageOriginX -= Fang_BackgroundImageCornerRadius;
            }
            
            if (arrowImageOriginX < Fang_BackgroundImageCornerRadius)
            {
                arrowImageOriginX += Fang_BackgroundImageCornerRadius;
            }
            
            // Setting arrow image for current arrow direction
            arrowImageView.image = Fang_DefaultTopArrowImage;
            
            break;
            
        case UIPopoverArrowDirectionDown:
            
            popoverImageHeight = self.bounds.size.height - Fang_ArrowHeight;
            
            arrowImageOriginX = roundf((self.bounds.size.width - Fang_ArrowBase) / 2.0f + self.arrowOffset);
            
            if ((arrowImageOriginX + Fang_ArrowBase) > (self.bounds.size.width - Fang_BackgroundImageCornerRadius))
            {
                arrowImageOriginX -= Fang_BackgroundImageCornerRadius;
            }
            
            if (arrowImageOriginX < Fang_BackgroundImageCornerRadius)
            {
                arrowImageOriginX += Fang_BackgroundImageCornerRadius;
            }
            
            arrowImageOriginY = popoverImageHeight;
            
            arrowImageView.image = Fang_DefaultBottomArrowImage;
            
            break;
            
        case UIPopoverArrowDirectionLeft:
            
            popoverImageOriginX = Fang_ArrowHeight;
            popoverImageWidth = self.bounds.size.width - Fang_ArrowHeight;
            
            arrowImageOriginY = roundf((self.bounds.size.height - Fang_ArrowBase) / 2.0f + self.arrowOffset);
            
            if ((arrowImageOriginY + Fang_ArrowBase) > (self.bounds.size.height - Fang_BackgroundImageCornerRadius))
            {
                arrowImageOriginY -= Fang_BackgroundImageCornerRadius;
            }
            
            if (arrowImageOriginY < Fang_BackgroundImageCornerRadius)
            {
                arrowImageOriginY += Fang_BackgroundImageCornerRadius;
            }
            
            arrowImageWidth = Fang_ArrowHeight;
            arrowImageHeight = Fang_ArrowBase;
            
            arrowImageView.image = Fang_DefaultLeftArrowImage;
            
            break;
            
        case UIPopoverArrowDirectionRight:
            
            popoverImageWidth = self.bounds.size.width - Fang_ArrowHeight;
            
            arrowImageOriginX = popoverImageWidth;
            arrowImageOriginY = roundf((self.bounds.size.height - Fang_ArrowBase) / 2.0f + self.arrowOffset);
            
            if ((arrowImageOriginY + Fang_ArrowBase) > (self.bounds.size.height - Fang_BackgroundImageCornerRadius))
            {
                arrowImageOriginY -= Fang_BackgroundImageCornerRadius;
            }
            
            if (arrowImageOriginY < Fang_BackgroundImageCornerRadius)
            {
                arrowImageOriginY += Fang_BackgroundImageCornerRadius;
            }
            
            arrowImageWidth = Fang_ArrowHeight;
            arrowImageHeight = Fang_ArrowBase;
            
            arrowImageView.image = Fang_DefaultRightArrowImage;
            
            break;
            
        default:
            break;
    }
    
    popoverBackgroundImageView.frame = CGRectMake(popoverImageOriginX, popoverImageOriginY, popoverImageWidth, popoverImageHeight);
    arrowImageView.frame = CGRectMake(arrowImageOriginX, arrowImageOriginY, arrowImageWidth, arrowImageHeight);
}

@end


@interface FangPopoverBackgroundView (UIAppearance)

@end

@implementation FangPopoverBackgroundView (UIAppearance)

+ (void)initialize {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [FangPopoverBackgroundView setArrowBase:12];
        [FangPopoverBackgroundView setArrowHeight:7];
        [FangPopoverBackgroundView setTintColor:[UIColor whiteColor]];
    });
}

@end

这是下拉框的代码

//
//  FangPopoverPresentationMenu.h
//  TESTBNR
//
//  Created by Vicktor on 2017/7/10.
//  Copyright © 2017年 Vicktor. All rights reserved.
//

#import <UIKit/UIKit.h>

@class FangPopoverPresentationMenuCell;

typedef void(^DidSelectRow)(NSIndexPath *indexPath);

@interface FangPopoverPresentationMenu : UITableViewController

/**
 箭头方向 是个枚举,可以自己控制方向
 */
@property (nonatomic, assign) UIPopoverArrowDirection permittedArrowDirections;

/**
 左侧图标
 */
@property (nonatomic, strong) NSArray<NSString *> * icons;

/**
 标题
 */
@property (nonatomic, strong) NSArray<NSString *> * titles;

/**
 cell高度
 */
@property (nonatomic, assign) CGFloat rowHeight;

/**
 箭头偏移量 UIBarButtonItem 的偏移量已经算好了 不用再算 如果出问题联系我
 */
@property (nonatomic, assign) CGFloat arrowOffset;

/**
 点击事件
 */
@property (nonatomic, copy) DidSelectRow didSelectRow;

/*
 popoverview 将要消失的回调
 */
@property (nonatomic, copy) void (^willDismissHandler)(void);

/**
 背景色
 */
//@property (nonatomic, strong) UIColor * popoverBackGroundColor;

/**
 初始化方法
 
 @param sender button
 @return self
 */
- (instancetype)initWithSender:(id)sender;

@end

@interface FangPopoverPresentationMenuCell : UITableViewCell

- (void)configureCellWithImageNamed:(NSString *)imageName text:(NSString *)text;

@end

//
//  FangPopoverPresentationMenu.m
//
//  Created by Vicktor on 2017/7/10.
//  Copyright © 2017年 Vicktor. All rights reserved.
//
//下面有些公司里用的categories,用原生的也可以
#import "FangPopoverPresentationMenu.h"
#import "UIColor+FangColor.h"
#import "NSString+FangSize.h"
#import "UILabel+FangLabel.h"
#import "FangPopoverBackgroundView.h"

#define KMAXROWS 5
#define PICWIDTH 20

static NSString * cellIdentifier = @"cellIdentifier";

@interface FangPopoverPresentationMenu()<UIPopoverPresentationControllerDelegate>
@property (nonatomic, assign) CGRect oldSourceRect;

@end

@implementation FangPopoverPresentationMenu


#pragma mark - LifeCycle

- (instancetype)initWithSender:(id)sender
{
    if (self = [super init]) {
        self.modalPresentationStyle = UIModalPresentationPopover;
        self.popoverPresentationController.backgroundColor = [UIColor whiteColor];
        self.popoverPresentationController.delegate = self;
        [self.popoverPresentationController setPopoverBackgroundViewClass:[FangPopoverBackgroundView class]];
        if ([sender isKindOfClass:[UIButton class]]) {
            self.popoverPresentationController.sourceView = sender;
            self.oldSourceRect = ((UIButton *)sender).bounds;
            CGRect newRect = self.oldSourceRect;
            if ([((UIButton *)sender).superview isKindOfClass:[UINavigationBar class]]) {
                newRect.origin.y += 22 - ((UIButton *)sender).frame.size.height/2.0;
            }
            self.oldSourceRect =  newRect;
            self.popoverPresentationController.sourceRect = self.oldSourceRect;
        } else if ([sender isKindOfClass:[UIBarButtonItem class]])
        {
            self.popoverPresentationController.barButtonItem = sender;
        }
        self.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
    self.tableView.separatorColor = UIColorMake(0xeeeeee);
    [self.tableView registerClass:[FangPopoverPresentationMenuCell class] forCellReuseIdentifier:cellIdentifier];
    
    if (self.icons.count != 0 && self.titles.count != self.icons.count) {
        NSAssert(NO, (@"图片和标题个数不相等,是设计木有给够吗?!"));
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.view.superview.layer.cornerRadius = 0;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    !self.willDismissHandler ? : self.willDismissHandler();
}

#pragma mark - Public

- (void)setPermittedArrowDirections:(UIPopoverArrowDirection)permittedArrowDirections {
    self.popoverPresentationController.permittedArrowDirections = permittedArrowDirections;
}

- (void)setArrowOffset:(CGFloat)arrowOffset
{
    _arrowOffset = arrowOffset;
    CGRect newSourceRect = self.oldSourceRect;
    newSourceRect.origin.y += _arrowOffset;
    self.popoverPresentationController.sourceRect = newSourceRect;
}

//- (void)setPopoverBackGroundColor:(UIColor *)popoverBackGroundColor
//{
//    _popoverBackGroundColor = popoverBackGroundColor;
//    self.popoverPresentationController.backgroundColor = _popoverBackGroundColor;
//}

#pragma mark - Overide

- (CGSize)preferredContentSize {
    __block CGFloat contenLength = 0.0f;
    [self.titles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (contenLength < [obj boundingWidthWithFontOfSize:15.0f]) {
            contenLength = [obj boundingWidthWithFontOfSize:15.0f];
        }
    }];

    contenLength += (self.icons.count > 0 ? PICWIDTH : - 20 - 18) + 18 + 20 + 39;
    
    return CGSizeMake(contenLength, MIN(KMAXROWS, self.titles.count) * (self.rowHeight ? : 50.0) - 2);
}


#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FangPopoverPresentationMenuCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[FangPopoverPresentationMenuCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellIdentifier];
    }
    [cell configureCellWithImageNamed:self.icons.count ? self.icons[indexPath.row] : nil text:self.titles[indexPath.row]];

    return cell;
}


#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self dismissViewControllerAnimated:NO completion:nil];
    
    if (self.didSelectRow) {
        self.didSelectRow(indexPath);
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.rowHeight ? : 50.0f;
}


#pragma mark - UIPopoverPresentationControllerDelegate

- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController
{
    return YES;
}

#pragma mark - UIAdaptivePresentationControllerDelegate

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}


@end

@interface FangPopoverPresentationMenuCell()
/**
 cellImageView
 */
@property (nonatomic, strong) UIImageView * iconImageView;
/**
 cell title
 */
@property (nonatomic, strong) UILabel * titleLabel;
@end

@implementation FangPopoverPresentationMenuCell


#pragma mark - LifeCycle

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.iconImageView];
        [self.contentView addSubview:self.titleLabel];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.iconImageView.translatesAutoresizingMaskIntoConstraints = NO;
    self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSDictionary *bindings = NSDictionaryOfVariableBindings(_iconImageView, _titleLabel);
    NSDictionary *metrics = nil;
    
    if (self.iconImageView.image) {
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-18-[_iconImageView(20)]-20-[_titleLabel]-39-|" options:0 metrics:metrics views:bindings]];
        
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.iconImageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.iconImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:20]];
    } else {
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-18-[_titleLabel]-18-|" options:0 metrics:metrics views:bindings]];
        
    }
    
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_titleLabel]|" options:0 metrics:metrics views:bindings]];
}

- (void)configureCellWithImageNamed:(NSString *)imageName text:(NSString *)text {
    if (imageName) {
        UIImage *image = [UIImage imageNamed:imageName];
//        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        self.iconImageView.image = image;
//        self.iconImageView.tintColor = [UIColor purpleColor];
    }

    self.titleLabel.text = text;
}


#pragma mark - Custom Accessors

- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    }
    return _iconImageView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithFrame:CGRectZero text:@"" fontOfSize:15.0f textColor:[UIColor fangBlackColor] textAlignment:(NSTextAlignmentLeft)];
    }
    return _titleLabel;
}

@end

调用

 FangPopoverPresentationMenu * menu = [[FangPopoverPresentationMenu alloc] initWithSender:sender];
    menu.icons = @[@"XFByuyuetuiguang.png",@"XFB_yuyuejingbiao.png",@"XFB_jinrijingbiao.png",@"XFB_zhanshizhongloupan.png",@"XFB_lishijingbiao.png"];
    menu.titles =@[@"楼盘预约",@"我的楼盘预约",@"竞标中楼盘",@"展示中的楼盘",@"我的竞标"];
//    menu.arrowOffset = 20.0f;
    menu.didSelectRow = ^(NSIndexPath *indexPath) {
        SL_Log(@"%@",indexPath);
        if (indexPath.row == 0) {
            XFBOrderBidViewController * vc = [[XFBOrderBidViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 1) {
            XFBReservateBidListController *VC = [[XFBReservateBidListController alloc] init];
            [self.navigationController pushViewController:VC animated:YES];
        }else if (indexPath.row == 2){
            XFBTodayBidListController *todayBidListVC = [[XFBTodayBidListController alloc] init];
            [self.navigationController pushViewController:todayBidListVC animated:YES];
        }else if (indexPath.row == 3){
            XFBListInDisplayController *listInDisplayVC = [[XFBListInDisplayController alloc]init];
            [self.navigationController pushViewController:listInDisplayVC animated:YES];
        }else if(indexPath.row == 4){
            XFBHistoryBidListController *historyBidListVC  = [[XFBHistoryBidListController alloc] init];
            [self.navigationController pushViewController:historyBidListVC animated:YES];
        }
    };
    [self presentViewController:menu animated:YES completion:nil];
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,723评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,080评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,604评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,440评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,431评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,499评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,893评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,541评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,751评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,547评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,619评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,320评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,890评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,896评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,137评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,796评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,335评论 2 342

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,364评论 25 707
  • 已然觉 带不走的思恋 躲在心底冲刷着无悔的誓言 怯怯的呼唤 面对明眸却止步不前 在犹豫中躲避逃离 独望繁星摇曳 ...
    守护者夜月星阅读 166评论 0 0
  • 参加21天爱上写作训练营的时间过了一大半,我的写作热情像抛物线一样,刚开始充满热情,信誓旦旦,连续3天完成...
    思故行阅读 98评论 1 1
  • 上一章|完美新娘(12) 刘丹为了感谢吴超帮了她一个大忙,特地在一家法国餐厅请吴超吃饭。吴超来餐厅的时候,还带了1...
    石如一阅读 318评论 0 2