IOS-封裝彈出框

來源:互聯網
上載者:User

IOS-封裝彈出框

1.封裝彈出框,包括設定彈出介面 設定彈出地點 彈出 消失等方法。大概封裝如下

 

#import @interface WBDropdownMenu : UIView@property (nonatomic,strong) UIView *contentView;@property (nonatomic,strong) UIViewController *cntController;+(instancetype)menu;-(void)showFrom:(UIView *)view;-(void)dismiss;@end

 

2.其實現如下:

 

#import "WBDropdownMenu.h"@interface WBDropdownMenu()@property (nonatomic ,weak) UIImageView *containerView;@end@implementation WBDropdownMenu-(UIImageView *)containerView{    if (!_containerView) {        UIImageView *iv=[[UIImageView alloc] init];        iv.image=[UIImage imageNamed:@"popover_background"];        iv.width=217;        [self addSubview:iv];        _containerView=iv;    }    return _containerView;}#pragme 初始化透明背景介面 和彈出框背景- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.frame=[UIScreen mainScreen].bounds;        self.backgroundColor=[UIColor clearColor];                [self containerView].userInteractionEnabled=YES;    }    return self;}+(instancetype)menu{    return [[WBDropdownMenu alloc] init];}#pragme 設定彈出框內容-(void)setContentView:(UIView *)contentView{    contentView.x=10;    contentView.y=15;        contentView.width=self.containerView.width-2 * contentView.x;    self.containerView.height=CGRectGetMaxY(contentView.bounds)+25;        [self.containerView addSubview:contentView];}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    [self dismiss];}#pragme 擴充內容view的另一種形式viewController-(void)setCntController:(UIViewController *)cntController{    _cntController=cntController;    [self setContentView:_cntController.view];}#pragme 設定顯示位置 該位置為windiow的絕對位置-(void)showFrom:(UIView *)view{   UIWindow *window= [[UIApplication sharedApplication].windows lastObject];    [window addSubview:self];    CGRect absRect=[view convertRect:view.bounds toView:self];    self.containerView.y=CGRectGetMaxY(absRect);    self.containerView.midX=CGRectGetMidX(absRect);    }-(void)dismiss{    [self removeFromSuperview];}@end

3.調用如下:

 

self.menu=[WBDropdownMenu menu];        WBDropdownViewController *tableViewController=[[WBDropdownViewController alloc] init];    tableViewController.tableView.height=44*3;        [self.menu setCntController:tableViewController];        [self.menu showFrom:target];


 

(大部分彈出框顯示為當前最前的window的內容,ios8中開始預設提供一個鍵盤window)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.