iOS5 - 使用故事板时模态视图控制器的大小

15
  1. 有没有办法调整使用故事板Segue模式呈现的视图控制器的大小?

  2. 如何使用翻转过渡从此模态视图控制器呈现另一个视图控制器?

如果我将其定义为Style=Modal,Presentation=Default,Transition=Flip Horizontal,则它看起来很奇怪(背景是白色)。

谢谢!


3
注意,你几乎总是希望一个 modalViewController 占据整个屏幕。这是因为当 modalViewController 出现后,位于其下方的 viewController 会被移除。因此,如果你试图使用 modalViewController 覆盖屏幕的一半,并打算让另一个 viewController 显示在其下面,这是行不通的。它会显示白色。我通过将一个视图添加为父视图的子视图来实现这个功能。 - bearMountain
您的评论在框架行为方面可能是正确的,但肯定应该由您应用程序的用户体验(UX)和对用户最有意义的内容来决定您“想要”的东西,而不是UI框架的底层内部工作。如果您的UX决定将模态UI显示在自定义对话框中,则仍需要查看对话框后面的内容。通过添加/删除对话框子视图(变暗,对话框内容)来手动处理自定义segue和解开segue可以轻松解决此问题,而无需删除先前控制器的视图。 - Marchy
这里有一个可行的解决方案:http://stackoverflow.com/questions/25250510/set-size-of-iphone-view-controller/25251507#25251507 - Katedral Pillon
3个回答

34

是的。在接口构建器中,选择视图控制器,在属性检查器中将大小设置为自由格式(从推断中)。然后选择视图并将其测量值设置为所需值。一个重要的注意点是在视图控制器中取消选中“从NIB调整视图大小”,否则视图将再次变成全屏。

如果背景颜色是唯一的问题,不确定第二个问题,您可以只是设置背景颜色吗?

希望这可以帮助您。


4
这种方法在我的情况下不起作用,尽管我按照您说的做了。有任何想法吗?谢谢。 - Ricardo
8
感谢您提到“从 NIB 调整视图大小”的复选框,否则我可能永远找不到它。+1 - DonnaLea
4
天啊,为什么我不能给一个帖子点赞多次?非常感谢,Scott! - Guillaume Algis
1
下投票;就我所知,这根本不起作用,在任何情况下都是如此。如果有人能提供重现此功能的步骤,我将撤回我的投票。 - Mark Amery
4
这个答案显然对很多人有用!如果你因为自己无法使其起作用而给它投反对票,那不符合 Stack Overflow 的精神,也不会得到你所期望的建设性回应! - Scott Sherwood
显示剩余3条评论

5

您可以使用自定义的UIStoryboardSegue调整模态呈现视图的大小。在Storyboard中,将segue设置为Custom而不是Modal。在Segue Class中,将其命名为您的UIStoryboardSegue覆盖。

要覆盖UIStoryboardSegue,您不需要在.h文件中添加任何内容。它会出现如下形式:

MyStoryboardSegue.h:

#import <UIKit/UIKit.h>
@interface MyStoryboardSegue : UIStoryboardSegue
@end

在MyStoryboardSegue.m文件中,代码应该是这样的:
#import "MyStoryboardSegue.h"

@implementation MyStoryboardSegue


- (void)perform
{
    id sourceController = self.sourceViewController;
    id destinationController = self.destinationViewController;
    UIView *destinationView = [destinationController view];
    CGFloat x, y, w, h;

    [destinationController setModalPresentationStyle:UIModalPresentationFormSheet];
    [destinationController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [sourceController presentViewController:destinationController animated:YES completion:nil];

/* 
You have to present the view first, and then resize it. That's because,
as far as I can tell, when you present your view modally, a new view is created
that covers the screen in a black semi-transparent mask to give the shadow effect,
and a container view is placed in the middle of this view that in turn holds the
view you are presenting. Your view automatically resizes to the size of this
container view, so that's the view you need to resize to make your view controller
appear the size you desire. You must present your modal view first 
because until you do, the container view doesn't exist. The following code
resizes the container view (which is now your modal view's superview) and then
resets the position of the container view to the center of the source view that
is presenting the modal view so everything looks nice and centered.
*/
    x = destinationView.superview.frame.origin.x;
    y = destinationView.superview.frame.origin.y;
    w = 400;  // Your desired modal view width
    h = 400;  // Your desired modal view height
    destinationView.superview.frame = CGRectMake(x, y, w, h);
    destinationView.superview.center = [[sourceController view] center];
}

@end

我已经在Xcode 5中使用UINavigationController完成了这个(如果有影响的话),但似乎除非我漏掉了什么,否则它不起作用... - Pantelis Proios

1

我也在尝试同样的事情,在storyBoard中我的ModalView比较小,但是在模拟器中它覆盖了整个屏幕...

我认为这在iPhone上无法实现...(我在iPad上进行了简单的测试,modal视图可以正常工作)。

我将尝试在iPhone上使用半透明视图,而不是使用Modal View。

希望这能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接