当改变方向时如何正确地调整UIView的大小?

9
当我改变方向时,我的文本视图不正确地调整大小,范围会向右、向左、向下或向上跳动...这是我的代码,请帮帮我...如何调整我的 fullText 视图大小..谢谢。
- (void)viewDidLoad
 { frameHor=CGRectMake(0,10,275,306);
    frameVer=CGRectMake(0, 51, 320, 351);
..
}



- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
        //self.view = portraitView;
        [self changeTheViewToPortrait:YES andDuration:duration];

    }
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
        //self.view = landscapeView;
        [self changeTheViewToPortrait:NO andDuration:duration];
    }
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];

    if(portrait){

        self.titleLabel.hidden=NO;
        self.backButton.hidden=NO;
        [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"det.png"]]];
        self.fullText.frame=CGRectMake(frameVer.origin.x, frameVer.origin.y, frameVer.size.width, frameVer.size.height);



    }
    else{   

        self.titleLabel.hidden=YES;
        self.backButton.hidden=YES;

        self.fullText.frame=CGRectMake(frameHor.origin.x, frameHor.origin.y, frameHor.size.width, frameHor.size.height);
        [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"T_iphoneauto.png"]]];




    }

    [UIView commitAnimations];
}
1个回答

20

你可以使用UIView的autoresizingMask属性。例如:

blackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin |
                                 UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | 
                                 UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

来自iOS文档:

讨论 当视图的边界发生变化时,该视图会根据每个子视图的自动调整大小掩码自动调整其子视图。您可以使用UIViewAutoresizing中描述的常量组合来指定此掩码的值,方法是使用C位或运算符。组合这些常量允许您指定视图的哪些尺寸应相对于父视图增长或缩小。此属性的默认值为UIViewAutoresizingNone,表示根本不应调整视图的大小。

当设置了同一轴上的多个选项时,默认行为是按比例在可伸缩部分之间分配大小差异。相对于其他可伸缩部分,灵活部分越大,它就越有可能增长。例如,假设此属性包括UIViewAutoresizingFlexibleWidth和UIViewAutoresizingFlexibleRightMargin常量,但不包括UIViewAutoresizingFlexibleLeftMargin常量,从而指示视图的左边距宽度固定,但视图的宽度和右边距可能会发生变化。因此,该视图看起来锚定在其父视图的左侧,而视图宽度和视图右侧的间隙都会增加。

如果自动调整行为不能为视图提供所需的精确布局,则可以使用自定义容器视图并覆盖其layoutSubviews方法以更精确地定位子视图。

更多信息请参见:UIView类参考


是的,谢谢,我了解自动调整大小掩码技巧,但在横向模式下,我隐藏了一些视图并应该将文本视图向上移动...如果我找不到更好的方法,我会停留在AutoResizingMask。 - Vladimir Stazhilov
当你有另一种视图模式时,你可以更改它的中心属性或仅更改框架。 - Nekto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接