我有一个 UITextView,纵向宽度为 500px。
我通过代码将其添加为子视图,但如果应用程序以横向模式打开,我无法在 viewdidapper 中获得正确的大小。
设备旋转后,它工作正常。如果打开景观没有。打开横向时如何计算此文本视图的正确尺寸?
谢谢
在 viewWillAppear 函数中添加通知程序
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
方向更改通知此功能
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}
并计算文本视图的大小
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//calculate the portrait textView size
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//calculate the landscape textView size
}}
我希望这有所帮助