繁体   English   中英

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

[英]How to resize UIView properly when changing orientation?

当我改变方向时,我的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];
}

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

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

来自iOS文档:

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

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

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

有关详细信息,请参阅此处: UIView类参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM