首先重写UIViewController方法:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return YES;}
?你也可以根据toInterfaceOrientation的不同值来判断是否允许旋转。这个传入参数有四种取值:
UIInterfaceOrientationLandscapeLeft 横向Home键在左UIInterfaceOrientationLandscapeRight 横向Home键在右UIInterfaceOrientationPortrait 正常UIInterfaceOrientationPortraitUpsideDown 反向Home键在上
可以在下面的方法中处理旋转后要重画的组件,或者重载另一个NIB文件。?
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {// 重新加载一个Nib文件if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) { [[NSBundle mainBundle] loadNibNamed:@"LoginViewLandscape" owner:self options:nil]; }else { [[NSBundle mainBundle] loadNibNamed:@"LoginView" owner:self options:nil]; }// 重写Toolbar// Set Toolbar UIBarButtonItem *newChat = [[UIBarButtonItem alloc] initWithTitle:@"新增" style:UIBarButtonItemStylePlain target:self action:@selector(createChat:)]; UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithTitle:@"刷新" style:UIBarButtonItemStylePlain target:self action:@selector(refresh:)]; UIBarButtonItem *deleteChat = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteChat:)]; self.deleteItem = deleteChat; self.deleteItem.enabled = NO; UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL]; if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { fixedItem.width = 155; }else { fixedItem.width = 75; } NSArray *toolBarItems = [[NSArray alloc] initWithObjects:newChat, fixedItem, refresh, fixedItem, self.deleteItem, nil]; [self setToolbarItems:toolBarItems]; [toolBarItems release]; [newChat release]; [deleteChat release]; [fixedItem release]; [refresh release];}
?
如果你使用了Interface Builder工具,并不一定要重写界面,工具有自动处理的方式。
方法:
1 选中你的组件
2 Command+3 打开View Size配置界面 你会看到有一项叫:Autosizing ?如下图:
3 设置组件自动缩放情况
??左边的框,中间有个小框,里面是指垂直和水平是否缩放,外面是指位置。根据各自应用调整,调整后在右边的小动画里能看到效果。
下面是两个实现了旋转响应的效果图,一个是重新加载了Nib文件,一个是用Interface Builder工具自动缩放。