gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:06:27 25 4
gpt4 key购买 nike

我正在开发一个内部应用程序;已要求应用程序的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com