zoukankan      html  css  js  c++  java
  • iOS UIPopoverView的使用

     UIViewController *contentViewController = [[UIViewController alloc] init];
        contentViewController.view.backgroundColor = [UIColor yellowColor];
        
        UIPopoverController *popController = [[UIPopoverController alloc] initWithContentViewController:contentViewController];
    //    popController.popoverBackgroundViewClass = [PopoverBackgroundView class];
        popController.popoverContentSize = CGSizeMake(300.0f, 100);
        self.popController = popController;
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(20, 20, 100, 30);
        button.backgroundColor = [UIColor redColor];
        [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        [contentViewController.view addSubview:button];
        
        [self.popController presentPopoverFromBarButtonItem:sender
                                   permittedArrowDirections:UIPopoverArrowDirectionUp
                                                   animated:YES];

    也可以自定义背景

    #import <UIKit/UIKit.h>
    @interface PopoverBackgroundView : UIPopoverBackgroundView
    @end
    #import "PopoverBackgroundView.h"
    #define kArrowBase 30.0f
    #define kArrowHeight 20.0f
    #define kBorderInset 0.0f
    
    @interface PopoverBackgroundView()
    @property (nonatomic, strong) UIImageView *arrowImageView;
    - (UIImage *)drawArrowImage:(CGSize)size;
    @end
    
    
    @implementation PopoverBackgroundView
    
    @synthesize arrowDirection  = _arrowDirection;
    @synthesize arrowOffset     = _arrowOffset;
    
    #pragma mark - Graphics Methods
    - (UIImage *)drawArrowImage:(CGSize)size
    {
        UIGraphicsBeginImageContextWithOptions(size, NO, 0);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [[UIColor clearColor] setFill];
        CGContextFillRect(ctx, CGRectMake(0.0f, 0.0f, size.width, size.height));
        
        CGMutablePathRef arrowPath = CGPathCreateMutable();
        CGPathMoveToPoint(arrowPath, NULL, (size.width/2.0f), 0.0f); //Top Center
        CGPathAddLineToPoint(arrowPath, NULL, size.width, size.height); //Bottom Right
        CGPathAddLineToPoint(arrowPath, NULL, 0.0f, size.height); //Bottom Right
        CGPathCloseSubpath(arrowPath);
        CGContextAddPath(ctx, arrowPath);
        CGPathRelease(arrowPath);
        
        UIColor *fillColor = [UIColor yellowColor];
        CGContextSetFillColorWithColor(ctx, fillColor.CGColor);
        CGContextDrawPath(ctx, kCGPathFill);
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
        
    }
    
    
    #pragma mark - UIPopoverBackgroundView Overrides
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor clearColor];
            
            //TODO: update with border image view
            UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
            self.arrowImageView = arrowImageView;
            [self addSubview:self.arrowImageView];
            
        }
        return self;
    }
    
    + (CGFloat)arrowBase
    {
        return kArrowBase;
    }
    
    + (CGFloat)arrowHeight
    {
        return kArrowHeight;
    }
    
    + (UIEdgeInsets)contentViewInsets
    {
        return UIEdgeInsetsMake(kBorderInset, kBorderInset, kBorderInset, kBorderInset);
    }
    
    + (BOOL)wantsDefaultContentAppearance
    {
        return NO;
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        
        //TODO: test for arrow UIPopoverArrowDirection
        CGSize arrowSize = CGSizeMake([[self class] arrowBase], [[self class] arrowHeight]);
        self.arrowImageView.image = [self drawArrowImage:arrowSize];
        
        self.arrowImageView.frame = CGRectMake(((self.bounds.size.width - arrowSize.width)- kBorderInset), 0.0f, arrowSize.width, arrowSize.height);
        
    }
  • 相关阅读:
    SpringMVC + Spring + MyBatis 学习笔记:遭遇order by 排序问题
    SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法
    SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇基础类型和日期类型报400错误解决方法
    SpringMVC + Spring + MyBatis 学习笔记:为MyBatis增加打印SQL功能 (最简化配置)
    [转]大部分人努力程度之低,根本轮不到拼天赋
    String内存陷阱简介
    同为程序员 为什么我的工资最低
    在程序员的眼里,用户是这样使用他们开发的软件的
    POI怎么和项目结合起来使用
    uploadify
  • 原文地址:https://www.cnblogs.com/keyan1102/p/4728771.html
Copyright © 2011-2022 走看看