ios - 下推所有 View 并添加自定义 View

标签 ios objective-c uiview ios7 uinavigationbar

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

像这个一样,日历是当前 View 中的导航栏;

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

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

最佳答案

使用下面的代码->

添加 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。方法二,重置keyWindow子View的frame。

对于方法二->
在 View 消失时重置 keyWindow 的 subview 的框架(移除 View Controller )。

-(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);
    }];
}

关于ios - 下推所有 View 并添加自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21916295/

相关文章:

ios - MBProgressHUD 禁用与 UITableViewController 的交互

ios - 在应用未显示内容时收到相应的推送通知时显示 Urban Airship 登陆页面

ios - CoreData MagicalRecord 保存方法在 iPhone5 上崩溃

ios - UIView 中的 UITableView

ios - MacinCloud 代理上的 VSTS Cordova iOS 构建失败 - "Cannot read property ' 未定义失败”

ios - NSMutableAttributedString 使用自定义字体崩溃

iOS Swift Realm 同步问题

ios - 弹出 ViewController 时停止执行 GCD

ios - View 在 iPhone X 横向模式下看起来不正确

ios - 将平移手势识别器限制为特定圆圈