I want to show a modal UIViewController
with a custom frame in iPad, centered on top of its parent view controller.
I tried using a form sheet but as far I know the frame and shadow effect can't be changed.
vc.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:cv animated:YES];
I also tried using a popover but as far as I know either I can't center it or I can't hide the arrow.
Is there another way to show modal view controllers? Is it possible to solve this problem by using form sheets or popovers?
There is no official way to do this however you can get the desired behavior by writing a custom view which keeps a reference or delegate to interact with its presenting view controller and adding it to the view hierarchy. To really get the modal feel you can also place a transparent overlay over the presenting controller just below your 'modal' view. I have done this in a number of apps and it usually works out great. You will likely need to make the custom overlay view so you can intercept touches and more elegantly animate its presentation.
My transparent overlay is usually something like this:
@protocol TransparentOverlayDelegate <NSObject>
@optional
- (void)transparentOverlayWillDismiss:(TransparentOverlay *)backgroundTouch;
- (void)transparentOverlayDidDismiss:(TransparentOverlay *)backgroundTouch;
@end
@interface TransparentOverlay : UIView {
id<TransparentOverlayDelegate> _delegate;
UIView *_contentView;
CGFloat _pAlpha;
}
@property(nonatomic, assign) id<TransparentOverlayDelegate> delegate;
@property(nonatomic, retain) UIView *contentView;
@property(nonatomic, assign) CGFloat pAlpha;
- (void)presentTransparentOverlayInView:(UIView *)view;
- (void)dismissTransparentOverlay:(BOOL)animated;
My custom modal view is usually something like this:
@protocol ModalViewDelegate <NSObject>
- (void)performSelectorOnDelegate:(SEL)selector;
@end
@interface ModalView : UIView {
id<ModalViewDelegate> _delegate;
}
@property(nonatomic, assign) id<ModalViewDelegate> delegate;
In my presenting view controller I would usually do the following :
- (void)presentModalController {
TransparentOverlay *to = [[[TransparentOverlay alloc] initWithFrame:self.view.bounds] autorelease];
to.delegate = self;
ModalView *mv = [[ModalView alloc] initWithFrame:CGRectMake(500, 500, 300, 300)];
mv.delegate = self;
to.contentView = mv;
[mv release];
[to presentTransparentOverlayInView:self.view];
}
Using the delegates defined on the two classes gives me pretty much open access to manipulate my presenting controller as well as my presentation and dismissal as desired. The only downside to this is when it is used on a view with a NavigationBar, as the bounds of the presenting controller's view will not contain the bounds of the NavigationBar leaving it open for interaction, there are ways to get around this but not of them are very pretty (adding to the navigation controller's view is one option).