Step1:宣告一個監聽事件,監聽UIDeviceOrientationDidChangeNotification方向轉換
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
Step2:撰寫function orientationChanged,針對不同規格刻版面。
{
CGFloat img_x=0.0f,img_y=65.0f,img_width=320.0f,img_height=40.0f;
CGFloat Seg_x=10.0f,Seg_y=70.0f,Seg_width=300.0f,Seg_height=30.0f;
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ){
NSLog(@"ipad");if (UIDeviceOrientationIsLandscape(deviceOrientation))
{
//do something is Landscape
img_x=0.0f,img_y=65.0f,img_width=1024.0f,img_height=50.0f;
Seg_x=253.0f,Seg_y=75.0f,Seg_width=500.0f,Seg_height=30.0f;
if (debug) NSLog(@"UIDeviceOrientationIsLandscape");
imageView.frame=CGRectMake(img_x,img_y,img_width,img_height);mySegmentedControl.frame=CGRectMake(Seg_x,Seg_y,Seg_width,Seg_height);
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation))
{
//do something is Portrait
img_x=0.0f,img_y=65.0f,img_width=768.0f,img_height=50.0f;
Seg_x=130.0f,Seg_y=75.0f,Seg_width=500.0f,Seg_height=30.0f;
if (debug) NSLog(@"UIDeviceOrientationIsPortrait");
imageView.frame=CGRectMake(img_x,img_y,img_width,img_height);mySegmentedControl.frame=CGRectMake(Seg_x,Seg_y,Seg_width,Seg_height);
}
}else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
NSLog(@"iphone");if (UIDeviceOrientationIsLandscape(deviceOrientation))
{
//do something is Landscape
img_x=0.0f,img_y=53.5f,img_width=568.0f,img_height=40.0f;Seg_x=138.0f,Seg_y=60.0f,Seg_width=300.0f,Seg_height=30.0f;
if (debug) NSLog(@"UIDeviceOrientationIsLandscape");
imageView.frame=CGRectMake(img_x,img_y,img_width,img_height);mySegmentedControl.frame=CGRectMake(Seg_x,Seg_y,Seg_width,Seg_height);
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation))
{
//do something is Portrait
img_x=0.0f,img_y=65.5f,img_width=320.0f,img_height=40.0f;Seg_x=10.0f,Seg_y=70.0f,Seg_width=300.0f,Seg_height=30.0f;
if (debug) NSLog(@"UIDeviceOrientationIsPortrait");
if (deviceOrientation==UIDeviceOrientationPortraitUpsideDown){
img_x=0.0f,img_y=53.5f,img_width=568.0f,img_height=40.0f;Seg_x=138.0f,Seg_y=60.0f,Seg_width=300.0f,Seg_height=30.0f;
}
imageView.frame=CGRectMake(img_x,img_y,img_width,img_height);
mySegmentedControl.frame=CGRectMake(Seg_x,Seg_y,Seg_width,Seg_height);
}
}
另外一種做法,可以說是比較懶惰的做法,只要刻一個版面就好,可以針對不同需求來作設定。
我想大家都應該知道ios6以上針對版面的轉換,已經更新了函式,現在是
- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return NO;
}- (NSUInteger)supportedInterfaceOrientations
{
//decide number of origination tob supported by Viewcontroller.
return UIInterfaceOrientationMaskAll;
}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//from here you Should try to Preferred orientation for ViewController
return UIInterfaceOrientationPortrait;
}但是呢?如果你用stroyboard來做版面的時候,會發現,似乎沒辦法針對每個版面進行不同的設定,例如你要再navigation controller 將 portrait 轉成 landscape,這個意思是第一個版面是portrait,第二個頁面是landscape,這樣就會遇到了困難,這樣怎麼解決呢?
Step1:在AppDelegate內的@end後加入下面程式碼
@implementation UINavigationController (Rotation_IOS6)
{
//NSLog(@"shouldAutorotate");
return [[self.viewControllers lastObject] shouldAutorotate];
}{
//NSLog(@"supportedInterfaceOrientations");
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}{
//NSLog(@"preferredInterfaceOrientationForPresentation");
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end
Step2:在你要的頁面中加入上述的函式即可
- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
//decide number of origination tob supported by Viewcontroller.
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//from here you Should try to Preferred orientation for ViewController
return UIInterfaceOrientationPortrait;
}


