IOS-encapsulation dialog box
1. encapsulate the pop-up box, including setting the pop-up page and setting the pop-up location to disappear. The encapsulation is as follows:
#import
@interface WBDropdownMenu : UIView@property (nonatomic,strong) UIView *contentView;@property (nonatomic,strong) UIViewController *cntController;+(instancetype)menu;-(void)showFrom:(UIView *)view;-(void)dismiss;@end
2. Its implementation is as follows:
# 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 initialize transparent background interface and pop-up box background-(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 settings pop-up box content-(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];} # Another form of pragme extended content view viewController-(void) setCntController :( UIViewController *) cntController {_ cntController = cntController; [self setContentView: _ cntController. view] ;}# set the display position of pragme to the absolute position of 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. The call is as follows:
self.menu=[WBDropdownMenu menu]; WBDropdownViewController *tableViewController=[[WBDropdownViewController alloc] init]; tableViewController.tableView.height=44*3; [self.menu setCntController:tableViewController]; [self.menu showFrom:target];
(Most pop-up boxes show the content of the current top window. A keyboard window is provided by default in ios8)