美文网首页
iOS下拉列表框 FangPopoverPresentation

iOS下拉列表框 FangPopoverPresentation

作者: vicktor_Liu | 来源:发表于2018-01-28 19:36 被阅读0次

    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];
    

    相关文章

      网友评论

          本文标题:iOS下拉列表框 FangPopoverPresentation

          本文链接:https://www.haomeiwen.com/subject/uduzaxtx.html