在iPad中显示带自定义框架的模态视图控制器 [英] Show modal view controller with custom frame in iPad

查看:90
本文介绍了在iPad中显示带自定义框架的模态视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iPad中使用自定义框架显示模态 UIViewController ,以其父视图控制器为中心。

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]; 
}

使用在这两个类上定义的委托给我几乎开放的操作权限我的演讲控制人以及我的演讲和解雇。唯一的缺点是当它在带有NavigationBar的视图上使用时,因为呈现控制器视图的边界将不包含NavigationBar的界限而使其保持打开以进行交互,有多种方法可以绕过它而不是它们非常漂亮(添加到导航控制器的视图是一个选项)。

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).

这篇关于在iPad中显示带自定义框架的模态视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆