IOS 6子视图在父视图中重叠工具栏

Posted

技术标签:

【中文标题】IOS 6子视图在父视图中重叠工具栏【英文标题】:IOS 6 subviews overlap toolbar in parent view 【发布时间】:2013-06-24 19:01:08 【问题描述】:

我正在关注学校的 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

当视图从蓝色 -> 黄色 -> 绿色切换回蓝色时,应用程序和逻辑都可以正常工作。如第一张图(BIDSubViewController 的 BIDBlueViewController 的 BlueView.xib 子视图)所示,每个视图与工具栏略有重叠。在我的一位同学的帮助下,我对我在 IB 中的所有模拟指标进行了两次和三次检查:

我是否在通过视图层次结构数组改组最顶层视图时使用了糟糕的做法,而不是通过书中的“removeFromParentViewController()”方法删除每个视图,或者是它们的另一个隐藏原因,导致子视图没有正确放置在里面/ 在父视图后面?

【问题讨论】:

【参考方案1】:

您的 UIToolbar 控件在其视图上的 Z-Order 位于您的蓝色视图控制器后面。由于您在 IB 中创建了它,因此您可以为其添加一个 IBOutlet,并将其视图带到父视图的顶部。您将 ViewControllers 作为子视图加载,因此它们都是您父级的 self.view 的子视图。

在您的标题定义中:

IBOutlet *toolBar UIToolbar;

在您的实施中脱颖而出。

[self.view bringSubviewToFront:toolBar];

您可以在添加蓝色控制器子视图后或在所有案例语句的末尾执行此操作。

【讨论】:

以上是关于IOS 6子视图在父视图中重叠工具栏的主要内容,如果未能解决你的问题,请参考以下文章

iOS 6/7 Deltas:仅适用于子视图?

模式视图在 ios 6.0 中调整大小,工具栏位于视图中间

ios 7子视图UINavigationBar与状态栏重叠..任何简单的解决方案..?

iPhone Dev - UIToolbar 进入窗口或视图?

片段内容重叠工具栏和底部导航视图

在 IOS 6.0 中:底部工具栏中的 UIBarButton 在呈现和关闭模式视图控制器后消失