2013-06-24 55 views
0

first subview (BIDBlueViewController's BlueView.xibIOS 6子視圖在父視圖重疊的工具欄

我下面的Apress的開端IOS 6所學校,我們被教授問以實施3次之間切換自定義算法(藍,黃,綠)每按一次工具欄中的「下一個視圖」按鈕。我的方法是添加在不同的索引子視圖以及將在分層結構中的視圖BIDSwitchViewController.m:

#import "BIDSwitchViewController.h" 
#import "BIDYellowViewController.h" 
#import "BIDBlueViewController.h" 
#import "BIDGreenViewController.h" 

NSInteger count; 

@interface BIDSwitchViewController() 

@end 

@implementation BIDSwitchViewController 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
     self.yellowViewController = [[BIDYellowViewController alloc] initWithNibName:@"YellowView" 
                       bundle:nil]; 
     self.greenViewController = [[BIDGreenViewController alloc] initWithNibName:@"GreenView" 
                      bundle:nil]; 
     self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:@"BlueView" 
                      bundle:nil]; 
     //the topmost view is the last one in the stack (2) 
     [self.view insertSubview:self.blueViewController.view atIndex:2]; 
     [self.view insertSubview:self.yellowViewController.view atIndex:1]; 
     [self.view insertSubview:self.greenViewController.view atIndex:0]; 
     [self.view setBackgroundColor:self.blueViewController.view.backgroundColor]; 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.blueViewController = [[BIDBlueViewController alloc] 
           initWithNibName:@"BlueView" bundle:nil]; 
    [self.view insertSubview:self.blueViewController.view atIndex:0]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated.; 
    self.blueViewController = nil; 
    self.yellowViewController = nil; 
    self.greenViewController = nil; 
} 

- (IBAction)switchViews:(id)sender 
{ 
    [UIView beginAnimations:@"View Flip" context:nil]; 
    [UIView setAnimationDuration:1.0]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationTransition:     
    UIViewAnimationTransitionFlipFromRight     
          forView:self.view cache:YES]; 
    switch (count) 
    { 
     case 0: 
      [self.view sendSubviewToBack:self.blueViewController.view]; 
      [self.view bringSubviewToFront:self.yellowViewController.view]; 
      [self.view setBackgroundColor:self.yellowViewController.view.backgroundColor]; 
      break; 
     case 1: 
      [self.view sendSubviewToBack:self.yellowViewController.view]; 
      [self.view bringSubviewToFront:self.greenViewController.view]; 
      [self.view setBackgroundColor:self.greenViewController.view.backgroundColor]; 
      break; 
     case 2: 
      [self.view sendSubviewToBack:self.greenViewController.view]; 
      [self.view bringSubviewToFront:self.blueViewController.view]; 
      [self.view setBackgroundColor:self.blueViewController.view.backgroundColor]; 
      break; 
    } 
    if (++count >= 3) 
    { 
     count = 0; 
    } 
    [UIView commitAnimations]; 
} 

@end 

這裏是BIDAppDelegate.m代碼,其中根視圖控制器被添加作爲BIDSwitchViewController的一個實例:

@implementation BIDAppDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 

    self.switchViewController = [[BIDSwitchViewController alloc] 
           initWithNibName:@"SwitchView" bundle:nil]; 
    UIView *switchView = self.switchViewController.view; 
    CGRect switchViewFrame = switchView.frame; 
    switchViewFrame.origin.y += [UIApplication 
           sharedApplication].statusBarFrame.size.height; 
    switchView.frame = switchViewFrame; 
    //[self.window addSubview:switchView]; 
    self.window.rootViewController = self.switchViewController; 


    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

而BIDSwitchViewController.h頭文件:

#import <UIKit/UIKit.h> 
@class BIDSwitchViewController; 
@interface BIDAppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) BIDSwitchViewController *switchViewController; 

@end 

該應用程序和邏輯的所有工作的視圖切換來回米藍色 - >黃色 - >綠色,並再次回藍。如第一張圖片(BIDBubViewViewController的BIDSubViewController的BlueView.xib子視圖)所示,每個視圖都略微與工具欄重疊。我有雙重和三重與我的一個同學的幫助下檢查所有的模擬指標在IB:

Interface Builder 1

enter image description here

我使用的不良做法在通過視圖層次洗牌最上面的意見數組,而不是通過本書的「removeFromParentViewController()」方法刪除每個視圖,或者是他們的另一個隱藏的原因,因爲子視圖不能正確地坐在父視圖的內部/後面?

回答

0

您的UIToolbar控件的視圖Z順序是在您的藍色視圖控制器後面。由於您是在IB中創建它的,因此可以爲其添加IBOutlet,並將其視圖添加到父視圖的頂部。您將ViewControllers加載爲子視圖,因此它們都是您父級的self.view的子視圖。

在你的頭定義:

IBOutlet *toolBar UIToolbar; 

移到前面在您的實現。

[self.view bringSubviewToFront:toolBar]; 

您可以在添加藍色控制器子視圖後或在您的所有病例語句結束後執行此操作。