我创建了一个新项目作为单视图应用程序。在视图控制器中添加了用于将视图坐标转换为窗口坐标的代码:
- (void) dumpFrame
{
CGRect bounds = self.view.bounds;
// UIView* rootView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
// I tried rootView instead of nil and self.view.superview instead of self.view but got {0,0} coordinate
CGPoint newCoord = [self.view convertPoint: bounds.origin
toView: nil];
bounds.origin.x = newCoord.x;
bounds.origin.y = newCoord.y;
NSLog(@"view.frame=%@", NSStringFromCGRect(bounds));
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation: fromInterfaceOrientation];
NSLog(@"===>didRotate");
[self dumpFrame];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation: toInterfaceOrientation duration: duration];
NSLog(@"===>willRotate");
[self dumpFrame];
}
我的日志:
===>wilRotate
view.frame={{0, 20}, {768, 1004}}
===>didRotate
view.frame={{748, 0}, {1024, 748}}
代替
{{0, 20}, {1024, 748}}
我有
{{748, 0}, {1024, 748}}
旋转后。我需要做什么才能获得 {0,20}?我会很高兴任何帮助。