如何在更改方向时正确调整UIView的大小?

时间:2011-08-30 07:27:07

标签: iphone objective-c ios

当我改变方向时,我的textview调整大小不正确,边界向右或向左或向下或向上跳跃......这是我的代码,请帮助我......我怎样才能调整我的全文UIView ..感谢

- (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 个答案:

答案 0 :(得分:19)

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

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

来自iOS文档:

  

讨论   当视图的边界发生变化时,该视图会根据每个子视图的自动调整蒙版自动调整其子视图的大小。通过组合使用C按位OR运算符在UIViewAutoresizing中描述的常量来指定此掩码的值。通过组合这些常量,您可以指定视图的哪些维度相对于超级视图应该增大或缩小。此属性的默认值为UIViewAutoresizingNone,表示视图不应调整大小。

     

当设置沿同一轴的多个选项时,默认行为是在柔性部分之间按比例分配尺寸差异。柔性部分相对于其他柔性部分越大,它就越可能生长。例如,假设此属性包含UIViewAutoresizingFlexibleWidth和UIViewAutoresizingFlexibleRightMargin常量,但不包括UIViewAutoresizingFlexibleLeftMargin常量,从而指示视图左边距的宽度是固定的,但视图的宽度和右边距可能会更改。因此,视图看起来锚定在其超视图的左侧,而视图宽度和视图右侧的间隙都增加。

     

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

有关详情,请点击此处:UIView class reference