代码之家  ›  专栏  ›  技术社区  ›  Jack

在随方向变化的代码中设置视图

  •  0
  • Jack  · 技术社区  · 15 年前

    我已经设置了一个应用程序,并使用“iPhone开发者食谱”中的一些基本代码创建了一个简单的视图。应用程序在纵向模式下正常工作,但旋转时,框的尺寸保持不变。这是我的密码:

    - (void)loadView {
        //Create the full application frame (minus the status bar) - 768, 1004
        CGRect fullRect = [[UIScreen mainScreen] applicationFrame]; 
    
        //Create the frame of the view ie fullRect-(tabBar(49)+navBar(44)) - 768, 911
        CGRect viewRect = CGRectMake(0, 64, fullRect.size.width, fullRect.size.height-93.0);
    
        //create the content view to the size
        contentView = [[UIView alloc] initWithFrame:viewRect]; 
        contentView.backgroundColor = [UIColor whiteColor];
    
        // Provide support for autorotation and resizing 
        contentView.autoresizesSubviews = YES;
        contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    
        self.view = contentView; 
        [contentView release];
    
        // reset the origin point for subviews. The new origin is 0,0 
        viewRect.origin = CGPointMake(0.0f, 0.0f);
    
        // Add the subviews, each stepped by 32 pixels on each side 
        UIView *subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 32.0f, 32.0f)]; 
        subview.backgroundColor = [UIColor lightGrayColor]; 
        [contentView addSubview:subview]; 
        [subview release];
    
        subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 64.0f, 64.0f)]; 
        subview.backgroundColor = [UIColor darkGrayColor]; 
        [contentView addSubview:subview]; 
        [subview release];
    
        subview = [[UIView alloc] initWithFrame:CGRectInset(viewRect, 96.0f, 96.0f)]; 
        subview.backgroundColor = [UIColor blackColor]; 
        [contentView addSubview:subview]; 
        [subview release];
    }
    

    旋转时,是否有任何方法可以使用新尺寸重新加载,或者有更好的方法来适应方向变化?

    谢谢

    2 回复  |  直到 15 年前
        1
  •  1
  •   TechZen    15 年前

    我可能误解了你的问题。“框”是指子视图吗?

    如果是这样,子视图在旋转时将保留其外观尺寸,因为它们是正方形。

    无论如何, viewDidLoad willRotateToInterfaceOrientation:duration: 和/或 didRotateFromInterfaceOrientation:

    要更改任何视图的尺寸,只需重置其框架即可。

        2
  •  1
  •   Ashish Chauhan    13 年前

    object.autoresizingMask=uiviewautoresizingflexibleleleftmargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | uiviewautoresizingflexibleftheright | uiviewautoresizingflexibleftmargin | uiviewAutoresizingflexibleftommargin |;