2011-08-30 108 views
9

当我改变方向我的textview调整不当,界限跳转到右边或左边或下来或上...这是我的代码,帮助我请....我如何调整我的fullText UIView ..谢谢如何在改变方向时正确调整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]; 
} 

回答

19

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

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

距离Ios文档:

讨论 当视图的边界改变,该视图将自动根据每个子视图的自动尺寸调整掩模调整大小它的子视图。您可以通过使用C按位或运算符组合UIViewAutoresizing中描述的常量来指定此掩码的值。通过组合这些常量,可以指定视图的哪些维度应相对于超级视图增长或缩小。该属性的默认值是UIViewAutoresizingNone,它表示视图不应该被调整大小。

如果设置了同一轴上的多个选项,则默认行为是在灵活部分之间按比例分配大小差异。柔性部分相对于其他柔性部分越大,其可能生长得越多。例如,假设此属性包含UIViewAutoresizingFlexibleWidth和UIViewAutoresizingFlexibleRightMargin常量,但不包含UIViewAutoresizingFlexibleLeftMargin常量,因此表示视图左边距的宽度是固定的,但视图的宽度和右边距可能会更改。因此,视图会出现在其超视图的左侧,而视图宽度和视图右侧的空白都会增加。

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

欲了解更多信息请看这里:UIView class reference

+0

是的,谢谢,我明白autoresizingMask技巧,但是当在景观我隐藏了一些意见和应取消的TextView起来......如果我没有找到更好的东西,我将停在AutoResizingMask –

+0

当您有其他视图模式时,您可以更改它的中心属性或更改框架。 – Nekto

相关问题