在通过窗口或导航控制器添加视图时,约束条件发生变化?
我有一个应用程序,我需要在所有应用程序上显示自定义状态栏。为此,我到didFinishLaunchingWithOptions:
方法写这个代码在通过窗口或导航控制器添加视图时,约束条件发生变化?
self.window.windowLevel = UIWindowLevelStatusBar;
然后在我的viewController创建了(0,0,宽度,20)帧图的图,并添加某些元素到它。
我尝试添加的窗口上面的这一观点是这样
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self.statusview setTranslatesAutoresizingMaskIntoConstraints:YES];
[appdelegate.window addSubview:statusview];
但它在左上角获取添加,而无需任何元素。但是,当我设置宽度并再次添加元素时,它就会显示出来。
有人能帮助我吗?
我认为你的问题是视图的宽度,确保宽度是正确的。
如果宽度合适并且问题仍然存在,请更新您的代码和屏幕截图。
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
_statusview.frame = CGRectMake(0, 50, self.view.frame.size.width, 45);
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
_statusview.frame = CGRectMake(0, 0, self.view.frame.size.width, 45);
}
- (void)viewDidLoad {
[super viewDidLoad];
_statusview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
_statusview.backgroundColor = [UIColor blackColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 100, 25);
btn.center = _statusview.center;
[btn setTitle:@"button" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor whiteColor]];
[_statusview addSubview:btn];
[_statusview setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.view addSubview:_statusview];
self.view.backgroundColor = [UIColor whiteColor];
}
把代码放在viewdidload中。那是你要的吗 ?
事实上,在窗口上方或视图上方,效果是相同的。 –
如果您始终需要视图,那么您只需要一个父控制器,并且所有子控制器都继承父类。 –
如果我设置框架,那么它不会显示内部elements.that我已告诉你 – hacker
我添加了宽度约束,当我添加此行statusview.frame = CGRectMake(0,0,self.view.frame.size.width,20);其中的元素不显示 – hacker
更改自我。 view.frame.size.width为[uiscreen mainscreen] .bounds.size.width,再试一次。 –
相同result.added正确但内部元素丢失 – hacker