我添加了一个webview到UIViewController。我认为我可以将它的大小与UIViewController的视图相匹配,但很快就意识到UIControllerView框架与屏幕方向无关,因此显示的框架与纵向模式相同(如果屏幕是错误的方式在景观中)。做UIInterfaceOrientationIsPortrait()
不起作用,因为如果设备躺平,那么即使屏幕旋转到横向,它仍然会将其报告为纵向。在旋转时调整UIViewController内的全屏视图的大小?
为了解决这个问题,我检查了状态栏的方向,然后使用UIScreen边界(也与旋转无关)计算出当前帧,然后删除状态栏的大小(如果它是可见的。
CGRect frame = [UIScreen mainScreen].bounds;
BOOL isPortrait;
UIDeviceOrientation orientation = (UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
isPortrait = YES;
}
else {
isPortrait = NO;
}
if (!isPortrait) {
frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.height, frame.size.width);
}
if ([UIApplication sharedApplication].statusBarHidden) {
return frame;
}
else {
return CGRectMake(frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height - 20);
}
现在我碰到的另一个问题 - 即使我建立了我的UIWebView为自动尺寸,该UIViewController的视图的框架是独立旋转的,所以它并没有改变,因此我的UIWebView不改变框架旋转后正确。
(奇怪的是,尽管有观点的UIViewController框架不改变,这个视图实际上正确地调整自身的大小)
我已经使用方法didRotateFromInterfaceOrientation:
,这并调整其大小尝试过,但毕竟只有旋转动画做。
使用willRotateToInterfaceOrientation:duration:
提出了与前面相同的问题 - UIInterfaceOrientationIsPortrait()
不是方向的真实反映。
有人设法做到这一点?
这帮了我:http://stackoverflow.com/questions/4920798/xcode-ios-autoresize-to-fill-a-view-explicit-frame-size-is-essential – 2013-10-14 13:57:19