UIViewControlle的view的Frame属性在什么时候被改变了?

2012-05-31 00:56:27 +08:00
 qichunren
- (IBAction)pop:(id)sender {
UIViewController *viewController = [[UIViewController alloc] init];

// view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 130)];
view.backgroundColor = [UIColor yellowColor];
viewController.view = view;
[view release];
[self presentModalViewController:viewController animated:TRUE];
[viewController release];
}

以上的代码中,在initWithFrame方法中,我不管把view的frame属性设置成什么,最终效果,这个弹出的控制器视图都是全屏的。所以我有些迷惑了,到底是什么过程改变了这个view的frame属性呢?

我在stackoverflow上问了这个问题,那老外的回答我还是没有找到有用信息呢。
http://stackoverflow.com/questions/10820037/how-did-ios-set-uiviewcontrollers-view-frame-before-shown
8859 次点击
所在节点    iDev
16 条回复
txx
2012-05-31 01:02:39 +08:00
问题出在 viewController.view = view上
这样 会无视掉view 的 frame 默认全屏....

试试吧 viewController.view 的 backGround Color 设为cleanColor 然后 addsubview吧啊
kejinlu
2012-05-31 01:02:49 +08:00
[self presentModalViewController:viewController animated:TRUE]; 不过这个方法目前的状态为DEPRECATED,建议使用新的吧,所看看苹果的文档。

presentModalViewController:animated:
Presents a modal view managed by the given view controller to the user. (Deprecated. Use presentViewController:animated:completion: instead.)

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
Parameters
modalViewController
The view controller that manages the modal view.
animated
If YES, animates the view as it’s presented; otherwise, does not.
Discussion

* On iPhone and iPod touch devices, the view of modalViewController is always presented full screen. On iPad, the presentation depends on the value in the modalPresentationStyle property. *

Sets the modalViewController property to the specified view controller. Resizes its view and attaches it to the view hierarchy. The view is animated according to the transition style specified in the modalTransitionStyle property of the controller in the modalViewController parameter.
kejinlu
2012-05-31 01:03:11 +08:00
看我上面 星号之间的句子
kejinlu
2012-05-31 01:06:47 +08:00
@txx 其实苹果非常不建议直接将一个viewController的view加入到另一个viewController的view我的hierarchy中,除非你清楚这样带来的后果. iOS 5.0之后新增了自定义容器试图控制器的接口,我前些日子写过一篇 关于容器的 http://geeklu.com/2012/05/custom-container-view-controller/
txx
2012-05-31 01:11:06 +08:00
@kejinlu 但是 他用的是UIView啊.......所以我直接 add SubView了...
qichunren
2012-05-31 01:12:57 +08:00
非常感谢 @txx 的指点,你说的
···
问题出在 viewController.view = view上
这样 会无视掉view 的 frame 默认全屏....
···
我不怎么明白,有相当的文档资料指出这点吗?
kejinlu
2012-05-31 01:15:49 +08:00
@qichunren 你将一个view设置成一个viewController的view,然后又通过presentModalViewController的方式将这个viewController作为modalViewController展现出来,苹果的文档说了
"
On iPhone and iPod touch devices, the view of modalViewController is always presented full screen. On iPad, the presentation depends on the value in the modalPresentationStyle property.
"
所以在使用presentModalViewController的时候,会去改变viewController的view的frame。u楼主明白了么?
kejinlu
2012-05-31 01:17:47 +08:00
@txx 哈哈 看快了,直接addSubview,设置好autoresizingMask,想必是可以的
txx
2012-05-31 01:19:02 +08:00
@qichunren 是这样
当你用[self presentModalViewController:viewController animated:TRUE];切换当前窗口的View Controller 的时候 ,这个新的Controller会铺满整个屏幕...
这个ViewController.view就类似于一个游戏的Scene。。。

于是就有了这个效果....
当然这些是我通过实践得来的 算是我YY的
官方文档我也没怎么查过...
我专攻cocos2d的.....偶尔写个cocoaTouch的东西哄自己玩罢了= =

关于cocoaTouch的东西 还是请教@kejinlu 比较好
qichunren
2012-05-31 01:24:37 +08:00
@txx 呀,我这个问题的提出其实就是看到cocos2d的测试中一个例子,把我搞糊涂了:
https://github.com/cocos2d/cocos2d-iphone/blob/release-2.0-rc1/tests/DirectorTest.m#L143

在143行中,虽然没有设置成全尺寸的frame,最终运行时,还是全屏效果,它是用[nav pushViewController:viewController animated:YES];这个方法来显示新创建的controller.

另外我通过调试发现 autoresizingMask 不是影响我这个问题的原因
txx
2012-05-31 01:28:18 +08:00
@qichunren 除非用interface builder 以外 我一般都不管 autoresizingMask 的.....
qichunren
2012-05-31 01:30:05 +08:00
@kejinlu 非常感谢。我想问一下,那除了presentModalViewController:viewController方法会将新的controller的视图铺满屏幕外,我下面的这个也会吗?
UINavigationController *nav = [app navController];
[nav pushViewController:viewController animated:YES];
我在看cocos2d一个测试例子时,发现是这样的.https://github.com/cocos2d/cocos2d-iphone/blob/release-2.0-rc1/tests/DirectorTest.m#L143
qichunren
2012-05-31 01:32:07 +08:00
InterfaceBuilder中的那个可视化的autoresizingMask设置 与相应的autoresizingMask属性代码对应有相关的资料吗?
txx
2012-05-31 01:35:01 +08:00
@qichunren nav这个要看 nav 本身的设置....有可能会留一个navigation Bar...
IB的资料啊......与其在这里问 你不如去看wwdc 讲如何使用ib。。。。
kejinlu
2012-05-31 01:42:22 +08:00
@qichunren UINavigationController push进一个viewController也是肯定会重设这个push进来的viewController的view的frame的,这个应该说成是充满UINavigationController的内容区域
clowwindy
2012-05-31 22:46:39 +08:00
楼主想做的是修改 modal view 的位置和大小吧。如果是 iPad 的话,modal view 就只有苹果定义的几种样式,其中有全屏的,有不全屏的。iPhone 则只有全屏的。

见 Presentation Styles for Modal Views 一节:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

如果想更好的控制位置和大小,自己加 subview,自己画边框吧。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/37896

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX