【问题标题】:Push down all views and add a custom view下推所有视图并添加自定义视图
【发布时间】:2014-02-20 18:19:12
【问题描述】:

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

和这个一样,日历是当前视图中的导航栏;

我累了

-(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 中的其余项目当然保持在同一位置,而且看起来我必须处理很多方向变化。

那么有没有一种可行的方法来下推应用程序中的每个视图并将该自定义视图放在顶部?

【问题讨论】:

    标签: ios objective-c uiview ios7 uinavigationbar


    【解决方案1】:

    使用下面的代码->

    添加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的子View的frames。

    对于方法 2->
    在 viewdisappear 时重置 keyWindow 的子视图的框架(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);
        }];
    }
    

    【讨论】:

    • [EventsListViewController setWindow:]: unrecognized selector sent to instance,我应该用什么来代替self.window?
    • 好的,差不多了,现在尝试使用方法2,但是如何重置keyWindow的子视图的框架,我尝试最终从当前窗口中删除所有内容。
    • 检查更新的答案。如果您同意,则接受答案。
    • 而不是使用最后一个对象,我使用标签属性认为这样更干净。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2011-09-13
    • 2013-04-07
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多