V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
qichunren
V2EX  ›  iDev

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

  •  
  •   qichunren ·
    qichunren · 2012-05-31 00:56:27 +08:00 · 8843 次点击
    这是一个创建于 4351 天前的主题,其中的信息可能已经有所发展或是发生改变。
    - (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
    16 条回复    1970-01-01 08:00:00 +08:00
    txx
        1
    txx  
       2012-05-31 01:02:39 +08:00
    问题出在 viewController.view = view上
    这样 会无视掉view 的 frame 默认全屏....

    试试吧 viewController.view 的 backGround Color 设为cleanColor 然后 addsubview吧啊
    kejinlu
        2
    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
        3
    kejinlu  
       2012-05-31 01:03:11 +08:00
    看我上面 星号之间的句子
    kejinlu
        4
    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
        5
    txx  
       2012-05-31 01:11:06 +08:00
    @kejinlu 但是 他用的是UIView啊.......所以我直接 add SubView了...
    qichunren
        6
    qichunren  
    OP
       2012-05-31 01:12:57 +08:00
    非常感谢 @txx 的指点,你说的
    ···
    问题出在 viewController.view = view上
    这样 会无视掉view 的 frame 默认全屏....
    ···
    我不怎么明白,有相当的文档资料指出这点吗?
    kejinlu
        7
    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
        8
    kejinlu  
       2012-05-31 01:17:47 +08:00
    @txx 哈哈 看快了,直接addSubview,设置好autoresizingMask,想必是可以的
    txx
        9
    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
        10
    qichunren  
    OP
       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
        11
    txx  
       2012-05-31 01:28:18 +08:00
    @qichunren 除非用interface builder 以外 我一般都不管 autoresizingMask 的.....
    qichunren
        12
    qichunren  
    OP
       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
        13
    qichunren  
    OP
       2012-05-31 01:32:07 +08:00
    InterfaceBuilder中的那个可视化的autoresizingMask设置 与相应的autoresizingMask属性代码对应有相关的资料吗?
    txx
        14
    txx  
       2012-05-31 01:35:01 +08:00
    @qichunren nav这个要看 nav 本身的设置....有可能会留一个navigation Bar...
    IB的资料啊......与其在这里问 你不如去看wwdc 讲如何使用ib。。。。
    kejinlu
        15
    kejinlu  
       2012-05-31 01:42:22 +08:00
    @qichunren UINavigationController push进一个viewController也是肯定会重设这个push进来的viewController的view的frame的,这个应该说成是充满UINavigationController的内容区域
    clowwindy
        16
    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,自己画边框吧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3223 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 00:39 · PVG 08:39 · LAX 17:39 · JFK 20:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.