下推所有视图并添加自定义视图

时间:2014-02-20 18:19:13

标签: ios objective-c uiview ios7 uinavigationbar

我正在开发一个内部应用程序;已经要求应用程序的徽标应位于导航栏上的所有视图的顶部。

与此类似,“日历”是此视图中的导航栏的当前值;

enter image description here

我累了

-(void)viewDidAppear:(BOOL)animated
{
    UIView *vMyCustomUIView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width,62)];
    vMyCustomUIView.backgroundColor=[UIColor colorWithHexString:@"#2896D5"];
    [[[UIApplication sharedApplication] keyWindow] addSubview:vMyCustomUIView];
    self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0, 62);
}

哪个有效,但它只是取代了导航栏的位置,self.view中的其余项目当然保持在同一位置,看起来我必须处理很多方向更改。

那么是否有可行的方法来推送应用中的每个视图并将该自定义视图置于顶部?

1 个答案:

答案 0 :(得分:3)

使用以下代码 - >

UIWindow *anotherWindow;添加为类属性(强引用)或ivar

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear: animated];
    UIWindow *window =[UIApplication sharedApplication].keyWindow;
    window.frame=CGRectOffset(window.frame, 0, 40);//move down the keyWindow.so navigation bar and views will come down
    // add another window on top.
    anotherWindow =[[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    anotherWindow.windowLevel =UIWindowLevelStatusBar;
    anotherWindow.hidden=NO;

    UIView*view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    view.backgroundColor =[UIColor greenColor];
    [anotherWindow addSubview:view];

}

方法2->

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear: animated];
    UIWindow *window =[UIApplication sharedApplication].keyWindow;
    [window.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
        obj.frame=CGRectOffset(obj.frame, 0, 40);
    }];

    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    view.backgroundColor =[UIColor greenColor];
    [self.view.window addSubview:view];
}

但是在解除了这个viewController之后,对于method1,不要忘记重置keyWindow的框架并删除anotherWindow。对于method2,重置keyWindow的子视图的帧。

对于方法2 - >
在viewdisappear上重置keyWindow的subViews框架(viewController删除)。

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    UIWindow *window =[UIApplication sharedApplication].keyWindow;
    [[window.subviews lastObject] removeFromSuperview];
    [window.subviews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
        obj.frame=CGRectOffset(obj.frame, 0, -40);
    }];
}