iOS——你如何控制模态视图控制器的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4231022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS -- how do you control the size of a modal view controller?
提问by William Jockusch
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?
我正在展示一个模态视图控制器。如果重要,它会从底部向上滚动。如何控制它占据屏幕的哪一部分?
EDIT: 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];
}
回答by atticus
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.
您可以修改视图控制器的框架,但是如果您使用 UIViewController 的 -presentModalViewController:animated: 方法,一旦您的模态视图完成动画到屏幕上,后面的视图将被卸载(假设您使用的是 iPhone)你会看到一个白色的屏幕,你的背景视图应该在那里。iOS 假定您的模态视图控制器将是一个全屏视图控制器,并转储另一个视图以节省内存。
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:
如果你真的想在屏幕的一部分上显示一个视图,你应该将 UIView(没有 UIViewController)作为子视图添加到当前 UIViewController 的视图中,然后自己在屏幕上设置动画。我认为这样的事情会在你的 UIViewController 类中工作,它将呈现视图:
// 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;
before the UIView animation block, and setting
在 UIView 动画块之前,并设置
myHalfView.alpha = 1.0;
inside the block after animating the center property.
在为 center 属性设置动画后的块内。
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.
完成后,您可以执行类似但相反的操作以将视图滑出屏幕。您可以向 UIView 动画块添加一个 animationDidStop 选择器,以便在视图滑出屏幕时收到通知,以便您可以将其从视图层次结构中删除。
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.
从美学的角度来看,你也应该小心你如何做到这一点,因为让视图向上滑动是一种标准行为,如果你的视图看起来像一个普通的视图但中途停止,用户可能会觉得(即使是短暂的)应用程序已经冻结。他们会弄清楚的,但如果处理不当,会给您的应用留下不好的印象。主要是,我会避免使用标准的全屏提示,例如在视图顶部包含 UINavigationController 以帮助用户了解正在发生的事情。iPhone 上的半页往往是 UIActionSheets,所以要朝那个方向思考。
回答by dsaw
That is nice, the above accepted answer explains a nice hack to present subViews which feel like ModalViews, but what if it is an iPad, and i can indeed give it a modalViewController which doesnt cover the entire screen.
很好,上面接受的答案解释了一个很好的技巧来呈现感觉像 ModalViews 的子视图,但是如果它是 iPad,我确实可以给它一个不覆盖整个屏幕的 modalViewController。
In case of iPads, I dont think the underneath view will be unloaded. ( because there are options where we can present the modalView on iPads, which dont cover the entire screen )
在 iPad 的情况下,我认为下面的视图不会被卸载。(因为有一些选项可以让我们在 iPad 上呈现 modalView,但不会覆盖整个屏幕)
ModalViewController in the end is a controller itself, and like any other controller has a root view, whose properties can be editted, if we can get hold of it.
ModalViewController 归根结底是一个控制器本身,并且像任何其他控制器一样有一个根视图,如果我们能够掌握它,它的属性可以被编辑。
Here is what will give you a custom frame of the ModalView :
这是将为您提供 ModalView 的自定义框架的内容:
MyViewController *viewController = [[MyViewController alloc] init];
viewConroller.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:viewController animated:YES];
//superView of viewController's view is modalViewController's view, which we were after
viewController.view.superview.frame = CGRectMake(x,y,w,h);
//x y w h - can have desired values.
回答by bugloaf
I would add to @dsaw's answer that the superview of the modal view does not seem to rotate its coordinate system in landscape mode. Here is the code that I used in my own app:
我会在@dsaw 的回答中补充说,模态视图的超级视图似乎没有在横向模式下旋转其坐标系。这是我在自己的应用程序中使用的代码:
MyViewController* modalVC = [[MyViewController alloc] init];
modalVC.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalVC animated:NO];
CGRect r = CGRectMake(self.view.bounds.size.width/2 - 236,
self.view.bounds.size.height/2 - 130,
472, 260);
r = [self.view convertRect:r toView:modalVC.view.superview.superview];
modalVC.view.superview.frame = r;
While the superview may not rotate itself with the iPad, it does seem to do the right thing and keep the modal view centered if I rotate the iPad after showing the modal view.
虽然超级视图可能不会随着 iPad 自身旋转,但如果我在显示模态视图后旋转 iPad,它似乎做正确的事情并保持模态视图居中。