2016-10-11 32 views
0

我有一個應用程序,我需要在所有應用程序上顯示自定義狀態欄。爲此,我到didFinishLaunchingWithOptions:方法寫這個代碼在通過窗口或導航控制器添加視圖時,約束條件發生變化?

self.window.windowLevel = UIWindowLevelStatusBar; 

然後在我的viewController創建了(0,0,寬度,20)幀圖的圖,並添加某些元素到它。

我嘗試添加的窗口上面的這一觀點是這樣

AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
[self.statusview setTranslatesAutoresizingMaskIntoConstraints:YES]; 
[appdelegate.window addSubview:statusview]; 

但它在左上角獲取添加,而無需任何元素。但是,當我設置寬度並再次添加元素時,它就會顯示出來。

有人能幫助我嗎?

回答

0

我認爲你的問題是視圖的寬度,確保寬度是正確的。

如果寬度合適並且問題仍然存在,請更新您的代碼和屏幕截圖。

+0

我添加了寬度約束,當我添加此行statusview.frame = CGRectMake(0,0,self.view.frame.size.width,20);其中的元素不顯示 – hacker

+0

更改自我。 view.frame.size.width爲[uiscreen mainscreen] .bounds.size.width,再試一次。 –

+0

相同result.added正確但內部元素丟失 – hacker

0
- (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中。那是你要的嗎 ?

+0

事實上,在窗口上方或視圖上方,效果是相同的。 –

+0

如果您始終需要視圖,那麼您只需要一個父控制器,並且所有子控制器都繼承父類。 –

+0

如果我設置框架,那麼它不會顯示內部elements.that我已告訴你 – hacker

相關問題