2011-05-21 45 views
0

我想在方向更改時調整我的工具欄的大小。 但我遇到了一個難題。我想調整我的工具欄的大小

我有兩個視圖控制器。 第一個視圖控制器有一個按鈕。當此按鈕觸摸時,第二個視圖將出現在顯示屏上。這些視圖控制器屬於導航控制器。

第一個視圖控制器使導航欄顯示。而第二個視圖控制器使導航欄隱藏。

在第一視圖控制器

- (void) viewWillAppear: (BOOL)animated { 

     [ super viewWillAppear: animated ]; 
     [ [ [ self navigationController ] navigationBar ] setHidden: NO ]; 

     UIButton *nextButton; 
     nextButton = [ UIButton buttonWithType: UIButtonTypeRoundedRect ]; 
     [ nextButton setFrame: CGRectMake(50.0, 50.0, 200.0, 150.0) ]; 
     [ nextButton setTag: 100 ]; 
     [ nextButton addTarget: self action: @selector(touchedNextButton:) forControlEvents: UIControlEventTouchUpInside ]; 
     [ [ self view ] addSubview: nextButton ]; 
} 

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { 

    // Return YES for supported orientations 
    BOOL iResult; 
    iResult = ((interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
         (interfaceOrientation == UIInterfaceOrientationLandscapeRight)); 
    return iResult; 

}在第二視圖控制器

- (void) didRotateFromInterfaceOrientation: (UIInterfaceOrientation)fromInterfaceOrientation { 

     UIInterfaceOrientation orientation = [ [ UIDevice currentDevice ] orientation ]; 

     UIButton *button = (UIButton *)[ [ self view ] viewWithTag: 100 ]; 

     switch (orientation) { 
      case UIInterfaceOrientationPortrait: 
       [ button setFrame: CGRectMake(50.0, 50.0, 200.0, 150.0) ]; 
       break; 
      case UIInterfaceOrientationLandscapeLeft: 
      case UIInterfaceOrientationLandscapeRight: 
       [ button setFrame: CGRectMake(50.0, 50.0, 200.0, 150.0) ]; 
       break; 
      default: 
       break; 
     } 
    } 

重要的方法

- (void) viewDidLoad { 

    [ [ [ self navigationController ] navigationBar ] setHidden: YES ]; 

    UIImage *imageBackArrow_iPhone = [ UIImage nonCachedImageNamed: @"backArrow_iPhone.png" ]; 
    NSMutableArray *toolbarItems = [ [ NSMutableArray alloc ] initWithCapacity: 5 ]; 
    [ toolbarItems addObject: [ [ [ UIBarButtonItem alloc ] initWithImage: imageBackArrow_iPhone style: UIBarButtonItemStylePlain target: self 
                     action: @selector(touchedBackArrowButton) ] autorelease ] ]; 
     UIToolbar *toolbar = [ [ UIToolbar alloc ] initWithFrame: CGRectMake(0.0, 416.0, 320.0, 44.0) ]; 
     [ toolbar setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin ]; 
     [ toolbar setItems: toolbarItems ]; 
     [ toolbarItems release ]; 
     [ [ self view ] addSubview: toolbar ]; 
     [ toolbar release ]; 
    } 
} 

其餘我來說很重要的方法第二視圖控制器中的方法類似於第一視圖控制器。

enter image description here

enter image description here

在第一視圖控制器開始對肖像模式後,我觸摸的下一個按鈕。改變方向工作正常。 (第一個截圖)

但是從第一個視圖控制器的縱向模式開始,我改變爲橫向模式。 然後我觸摸下一個按鈕,這使得奇怪的佈局。 (第二個截圖)

我不知道是什麼導致了這個問題。

請讓我知道您的經驗。你的建議會讓我清醒。 謝謝您的閱讀。

回答

0

代碼看起來不太可能....但 你可以嘗試在你的視圖 - 控制你的viewWillAppear中的方法調整大小的東西,以及。 這可能有所幫助。

0

你爲什麼不只是使用自動尺寸口罩?

+0

我正在使用自動調整掩碼。 [工具欄setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin]; – MonsterK 2011-05-21 07:05:35

相關問題