2012-08-01 23 views
0

我想調整一個UINavigationBar的與的.xib文件連接時,在我的iPad應用的方向變化,現在我會做這樣一個UITableView:如何調整就改變方向視圖組件

- (void)viewWillAppear:(BOOL)animated { 

UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
if (UIInterfaceOrientationIsLandscape(currentOrientation)) { 
    [self.seriesTable setFrame:CGRectMake(self.seriesTable.frame.origin.x, self.seriesTable.frame.origin.y, 425, self.seriesTable.frame.size.height)]; 
    [self.myNavBar setFrame:CGRectMake(self.myNavBar.frame.origin.x, self.myNavBar.frame.origin.y, 425, self.myNavBar.frame.size.height)]; 
} else { 
    [self.seriesTable setFrame:CGRectMake(self.seriesTable.frame.origin.x, self.seriesTable.frame.origin.y, 670, self.seriesTable.frame.size.height)]; 
    [self.myNavBar setFrame:CGRectMake(self.myNavBar.frame.origin.x, self.myNavBar.frame.origin.y, 670, self.myNavBar.frame.size.height)]; 
} 
} 

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { 
    NSLog(@"landscape"); 
    [self.seriesTable setFrame:CGRectMake(self.seriesTable.frame.origin.x, self.seriesTable.frame.origin.y, 425, self.seriesTable.frame.size.height)]; 
    [self.myNavBar setFrame:CGRectMake(self.myNavBar.frame.origin.x, self.myNavBar.frame.origin.y, 425, self.myNavBar.frame.size.height)]; 

} else { 
    NSLog(@"portrait"); 
    //[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, 678, self.view.frame.size.height)]; 
    [self.seriesTable setFrame:CGRectMake(self.seriesTable.frame.origin.x, self.seriesTable.frame.origin.y, 670, self.seriesTable.frame.size.height)]; 
    [self.myNavBar setFrame:CGRectMake(self.myNavBar.frame.origin.x, self.myNavBar.frame.origin.y, 670, self.myNavBar.frame.size.height)]; 

} 
} 

當應用程序開始框架大小是正確的,但是當我改變方向去在將旋轉..方法,但框架不改變大小,並調整它我必須切換到另一個UITabBar選項卡,然後返回到該視圖,顯然傳遞viewwillappear方法並調整視圖的大小,但是當方向改變什麼都沒有發生時,我該怎麼辦?

回答

1

下面的代碼,這將幫助你改變基於取向

-(void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (orientationchangefunction:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
    [self orientationchngfn]; 

} 



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    if(interfaceOrientation == UIInterfaceOrientationPortrait) 
    { 
     orientseason=0; 
    } 
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
    { 
     orientseason=1; 
    } 
    if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     orientseason=1; 
    } 
    if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     orientseason=0; 
    } 
    if(orientseason==0) 
    { 
    } 
    else if(orientseason==1) 
    { 

    } 

    return YES; 
} 

-(void) orientationchangefunction:(NSNotification *) notificationobj 
{ 

    [self performSelector:@selector(orientationchngfn) withObject:nil afterDelay:0.0]; 
} 
-(void) orientationchngfn 
{ 
    UIDeviceOrientation dorientation =[UIDevice currentDevice].orientation; 

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 

     if(UIDeviceOrientationIsPortrait(dorientation)) 

     { 
      orientseason=0; 
     } 
     else if (UIDeviceOrientationIsLandscape(dorientation)) 
     { 
      orientseason=1; 
     } 
     if(orientseason==0) 
     { 
      webview4Html.frame=CGRectMake(5, 44, 310, 419); 


     } 
     else if(orientseason==1) 
     { 
      webview4Html.frame=CGRectMake(5, 44, 470, 276); 

     } 

    } 
    else { 
     if(UIDeviceOrientationIsPortrait(dorientation)) 

     { 
      orientseason=0; 
     } 
     else if (UIDeviceOrientationIsLandscape(dorientation)) 
     { 

      orientseason=1; 
     } 
     if(orientseason==0) 
     { 
      webview4Html.frame=CGRectMake(5, 44, 758, 940); 


     } 
     else if(orientseason==1) 
     { 
      webview4Html.frame=CGRectMake(5, 44, 1014, 684); 

     } 
    } 

} 
+0

做你讀過我的問題的網頁視圖大小?我也是這樣,爲什麼你比我好? – Piero 2012-08-01 12:51:03

+0

我不知道如何,但它的作品!非常感謝你! – Piero 2012-08-01 13:08:13

0

假設調整心不是太奇怪了,這將是更容易使用autoresizing masks

+0

你給我的鏈接是mac,我不明白你是什麼意思... – Piero 2012-08-01 12:58:28

+0

對不起,我只是google了一個鏈接。嘗試[this](http://developer.apple.com/library/ios/#documentation/windowsviews/conceptual/viewpg_iphoneos/CreatingViews/CreatingViews.html) – wattson12 2012-08-01 12:59:40

+0

autoresizing mask是UIView上的一個屬性,它定義瞭如何調整視圖大小當一個視圖需要佈局時(通常是通過旋轉) – wattson12 2012-08-01 13:00:19

相關問題