您的位置:首页 > 移动开发 > IOS开发

ios 程序窗口适应设备方向改变

2011-12-02 16:10 197 查看
//在初始化里注册窗口改变通知,设备窗口改变会发送此通知
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(change:) name:UIDeviceOrientationDidChangeNotification object:nil];

}

returnself;

}

-(void) change:(NSNotification*)nt

{

CGFloat width = [[UIScreen mainScreen]bounds].size.width * [[UIScreen mainScreen]scale];

CGFloat height = [[UIScreen mainScreen]bounds].size.height * [[UIScreen mainScreen]scale]; //乘以缩放系数

UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

switch (orientation) {

  case UIDeviceOrientationPortrait:

  case UIDeviceOrientationPortraitUpsideDown:

    self.view.frame = CGRectMake(0, 0, width, height);

    break;

  case UIDeviceOrientationLandscapeLeft:

  case UIDeviceOrientationLandscapeRight:

    self.view.frame = CGRectMake(0, 0, height, width);

    break;

  default:

break;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: