当我改变方向时,我的 textview 调整大小不正确,边界跳到右边或左边或向下或向上......这是我的代码,请帮助我......我如何调整我的 fullText 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 文档:
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. 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.
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. 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.
有关更多信息,请查看此处:UIView class reference
关于iphone - 更改方向时如何正确调整 UIView 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7239926/