iOS - 如何控制模态视图控制器的大小? [英] iOS -- how do you control the size of a modal view controller?

查看:526
本文介绍了iOS - 如何控制模态视图控制器的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在呈现一个模态视图控制器。如果重要,它会从底部向上滚动。如何控制它所占据的屏幕部分?

I am presenting a modal view controller. If it matters, it is scrolling up from the bottom. How can I control what portion of the screen it occupies?

编辑:我在模态视图控制器中有以下内容。它没有帮助。

I have the following in the modal view controller. It's not helping.

- (void)viewDidLoad {
    TestResultView *trv = [[TestResultView alloc]initWithTest: [Model m].currentTest];
    self.view = trv;
    trv.frame = CGRectMake(0, 320, 320, 160);
    [trv release];
    [super viewDidLoad];
}


推荐答案

你可以修改框架视图控制器,但是如果你正在使用UIViewController的-presentModalViewController:animated:方法,一旦你的模态视图完成动画到屏幕上就会卸载后面的视图(假设你在iPhone上),你会看到一个白色屏幕,你的背景应该是。 iOS假设你的模态视图控制器将是一个全屏视图控制器,并转储另一个视图以节省内存。

You can modify the frame of the view controller, but if you're using UIViewController's -presentModalViewController:animated: method, the view behind will be unloaded once your modal view is finished animating onto the screen (This assumes you're on an iPhone) and you'll see a white screen where your background view should be. iOS assumes that your modal view controller will be a full-screen view controller, and dumps the other view to save memory.

如果你真的想要显示部分视图在屏幕上,您应该将UIView(无UIViewController)作为子视图添加到当前的UIViewController视图中,然后自己在屏幕上为其设置动画。我认为这样的东西可以在你的UIViewController类中工作,它将呈现视图:

If you really want to show a view over part of the screen, you should instead add the UIView (no UIViewController) to your current UIViewController's view as a subview, and then animate it onscreen yourself. I think something like this would work in your UIViewController class that will present the view:

// Add the view as a subview and position it offscreen just below the current view
UIView *myHalfView = [[UIView alloc] initWithFrame:someAppropriateFrame];
[self.view addSubview:myHalfView];
CGRect offScreenFrame = myHalfView.bounds;
offScreenFrame.origin = CGPointMake(0.0, CGRectGetMaxY(self.view.frame));

// Now animate the view upwards
[UIView beginAnimations:nil context:nil];
// Move the view upwards the height of your sliding view so it's entirely onscreen
myHalfView.center = CGPointMake(myHalfView.center.x, myHalfView.center.y - myHalfView.bounds.size.height);
[UIView commitAnimations];
[myHalfView release];

对于奖励积分,您可以通过设置

For bonus points, you could fade the view in by setting

myHalfView.alpha = 0.0;

并设置

myHalfView.alpha = 1.0;

完成后,您可以执行类似操作,但反过来将视图滑动到屏幕外。您可以将一个animationDidStop选择器添加到UIView动画块,以便在视图滑出屏幕时收到通知,以便您可以从视图层次结构中将其删除。

When you're done, you can do something similar but in reverse to slide the view offscreen. You can add an animationDidStop selector to the UIView animation block to be notified when the view has slid off screen so that you can remove it from the view hierarchy.

来自美学从这个角度来看,你也应该小心如何做到这一点,因为视图向上滑动是一种标准行为,如果你的视图看起来像普通视图但是中途停止,用户可能会(甚至简短地)感觉应用程序已经冻结。他们会搞清楚,但如果不仔细处理,它会给你的应用留下不好的感觉。主要是,我会避免使用标准的全屏幕提示,例如在视图顶部包含UINavigationController,以帮助用户了解正在发生的事情。半张纸往往是iPhone上的UIActionSheets,所以请朝这个方向思考。

From an aesthetic point of view, you should also be careful how you do this since having a view slide up is a standard behavior, and if your view looks like a normal view but stops halfway, users may feel (even briefly) that the app has frozen. They'll figure it out, but it will leave a bad feeling about your app if not handled carefully. Mainly, I would avoid using standard full-screen cues like including a UINavigationController at the top of your view to help users understand what's going on. Half-sheets tend to be UIActionSheets on the iPhone, so think in that direction.

这篇关于iOS - 如何控制模态视图控制器的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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