[Objective-C] Modal view in Navigation and TabBar controller projects

A simple way to show in your project a view that is ever modal (with opacity view on background), like the UIAlertView, in fact it is UIAlertView that we use, subclassing it!

How to?

> Create a new Category of UIAlertView

#import <uikit/UIKit.h>

@interface UIAlertView (Modal)
- (void) showCustom;
#import "UIAlertView+Modal.h"

@implementation UIAlertView (Modal)

-(void)showCustom {
	UIView *ModalView = [[UIView alloc] initWithFrame:CGRectMake(-10, -50, 300, 200)];
	[ModalView setBackgroundColor:[UIColor redColor]];

	UILabel *TextLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 250, 20)];
	[TextLabel setText:@"I'm a label on a modal view!"];
	[TextLabel setTextColor:[UIColor blackColor]];
	[TextLabel setBackgroundColor:[UIColor clearColor]];
	[ModalView addSubview:TextLabel];
	[TextLabel release];

	UIButton *CloseBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[CloseBtn setFrame:CGRectMake(100, 120, 100, 40)];
	[CloseBtn setTitle:@"Close" forState:UIControlStateNormal];
	[ModalView addSubview:CloseBtn];

	[self addSubview:ModalView];
	[ModalView release];

	[self show];


Now, include the category header in your project and call alert view when you want, in this way:

UIAlertView *alert = [[UIAlertView alloc] 
	delegate: nil 

// use showCustom if you want the modal view!
[alert showCustom];
[alert release];


Rif: albertopasca.it

Alberto Pasca
Latest posts by Alberto Pasca (see all)