iOS-封装弹出框

<pre name="code" class="objc">1.封装弹出框,包括设置弹出界面 设置弹出地点 弹出 消失等方法。大概封装如下

#import<uikit uikit.h="">
 
@interfaceWBDropdownMenu : UIView
 
@property(nonatomic,strong) UIView *contentView;
 
@property(nonatomic,strong) UIViewController *cntController;
 
+(instancetype)menu;
 
-(void)showFrom:(UIView *)view;
 
-(void)dismiss;
 
@end</uikit>

 

2.其实现如下:

#import"WBDropdownMenu.h"
@interfaceWBDropdownMenu()
 
@property(nonatomic ,weak) UIImageView *containerView;
 
@end
 
@implementationWBDropdownMenu
 
-(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 = [superinitWithFrame:frame];
    if(self) {
        self.frame=[UIScreen mainScreen].bounds;
        self.backgroundColor=[UIColor clearColor];
         
        [self containerView].userInteractionEnabled=YES;
    }
    returnself;
}
 
+(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
)


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统的UIAlert不太好用,写起来感觉太零散. 使用方法:case 0: [LTAlertView showTitle:@"LTAlert" message:@"我是普通alert" ButtonTitles:@[@"确认",@"取消"] OnTapBlock:^(LTAlertView* alert,NSInteger num) { NSLog(@"点击了第%d个按钮",num); }]; break; case 1: [LTAlertView showConfigBlock:^(LTAlertView *alertView) { alertView.alertViewStyle = UIAlertViewStyleSecureTextInput; } Title:@"LTAlert" message:@"带密码输的alert" ButtonTitles:@[@"确定",@"取消"] OnTapBlock:^(LTAlertView* alert,NSInteger num) { NSString* str = [alert textFieldAtIndex:0].text; NSLog(@"输入的文字是%@,点击了第%d个按钮",str,num); }]; break; case 2: [LTAlertView showConfigBlock:^(LTAlertView *alertView) { alertView.alertViewStyle = UIAlertViewStylePlainTextInput; } Title:@"LTAlert" message:@"带输入的alert" ButtonTitles:@[@"确定",@"取消"] OnTapBlock:^(LTAlertView* alert,NSInteger num) { NSString* str = [alert textFieldAtIndex:0].text; NSLog(@"输入的文字是%@,点击了第%d个按钮",str,num); }]; break; case 3: [LTAlertView showConfigBlock:^(LTAlertView *alertView) { alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; alertView.backgroundColor = [UIColor redColor]; } Title:@"LTAlert" message:@"弹出带输入的alert" ButtonTitles:@[@"确定",@"取消"] OnTapBlock:^(LTAlertView* alert,NSInteger num) { NSString* accountStr = [alert textFieldAtIndex:0].text; NSString* passwordStr =[alert textFieldAtIndex:1].text; NSLog(@"账号是%@,密码是%@",accountStr,passwordStr); }]; break;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值