简体   繁体   English

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

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

When I change orientation my textview resizes improperly, the bounds jump to the right or to the left or down or up... here is my code, help my please.... How can I resize my fullText UIView.. thanks 当我改变方向时,我的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];
}

You can use autoresizingMask property of your UIView. 您可以使用UIView的autoresizingMask属性。 For example: 例如:

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

From iOS documentation: 来自iOS文档:

Discussion When a view's bounds change, that view automatically resizes its subviews according to each subview's autoresizing mask. 讨论当视图的边界发生变化时,该视图会根据每个子视图的自动调整蒙版自动调整其子视图的大小。 You specify the value of this mask by combining the constants described in UIViewAutoresizing using the C bitwise OR operator. 通过组合使用C按位OR运算符在UIViewAutoresizing中描述的常量来指定此掩码的值。 Combining these constants lets you specify which dimensions of the view should grow or shrink relative to the superview. 通过组合这些常量,您可以指定视图的哪些维度相对于超级视图应该增大或缩小。 The default value of this property is UIViewAutoresizingNone, which indicates that the view should not be resized at all. 此属性的默认值为UIViewAutoresizingNone,表示根本不应调整视图的大小。

When more than one option along the same axis is set, the default behavior is to distribute the size difference proportionally among the flexible portions. 当设置沿同一轴的多个选项时,默认行为是在柔性部分之间按比例分配尺寸差异。 The larger the flexible portion, relative to the other flexible portions, the more it is likely to grow. 柔性部分相对于其他柔性部分越大,它就越可能生长。 For example, suppose this property includes the UIViewAutoresizingFlexibleWidth and UIViewAutoresizingFlexibleRightMargin constants but does not include the UIViewAutoresizingFlexibleLeftMargin constant, thus indicating that the width of the view's left margin is fixed but that the view's width and right margin may change. 例如,假设此属性包含UIViewAutoresizingFlexibleWidth和UIViewAutoresizingFlexibleRightMargin常量,但不包括UIViewAutoresizingFlexibleLeftMargin常量,从而指示视图左边距的宽度是固定的,但视图的宽度和右边距可能会更改。 Thus, the view appears anchored to the left side of its superview while both the view width and the gap to the right of the view increase. 因此,视图看起来锚定在其超视图的左侧,而视图宽度和视图右侧的间隙都增加。

If the autoresizing behaviors do not offer the precise layout that you need for your views, you can use a custom container view and override its layoutSubviews method to position your subviews more precisely. 如果自动调整行为不提供视图所需的精确布局,则可以使用自定义容器视图并覆盖其layoutSubviews方法以更精确地定位子视图。

For more info look here: UIView class reference 有关详细信息,请参阅此处: UIView类参考

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

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