<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
)
iOS-封装弹出框
最新推荐文章于 2022-10-15 23:52:15 发布