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

查看:16
本文介绍了在 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天全站免登陆