2017-12-25 27 views
1

검은 색 상태 표시 줄에 흰색 텍스트가 필요합니다. 뷰 컨트롤러는 네비게이션 컨트롤러에 내장되어있는 경우 내가 사용 일반 접근 방식은 내가UINavigationController에 대한 상태 표시 줄 배경 사용자 정의

을 빛 스타일의 상태 표시 줄에 사용자 정의 블랙 뷰를 작성하고 뷰 컨트롤러

CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height; 
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)]; 
view.backgroundColor = [UIColor blackColor]; 
[self.view addSubview:view]; 

의 상단에 추가 할 필요가있다

CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height; 
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)]; 
view.backgroundColor = [UIColor blackColor]; 
[self.navigationController.navigationBar addSubview:view]; 

그러나 이것은 매우 우아한 해결책이 아닌 것 같습니다. 어쩌면 더 나은 접근법이 있을까요?

답변

2

keywindow에서보기를 추가하여 할 수있는 한 가지 방법입니다. 앱이 UINavigationController 또는 UIViewController에서 실행되는지는 중요하지 않습니다. 나는 아래와 같이 UIStatusBarStyle의 확장을함으로써 이것을했다.

extension UIStatusBarStyle { 
    static func setbackground() { 
     let view = UIView.init(frame: UIApplication.shared.statusBarFrame) 
     view.backgroundColor = UIColor.black 
     UIApplication.shared.keyWindow?.addSubview(view) 
    } 
} 

이제이 메소드를 didFinishLaunchingWithOptions 메소드에서 appdelegate로 호출하십시오. 또한

UIStatusBarStyle.setbackground() 

, 당신의 Info.plist 파일에 사용자가 설정했는지 확인

View controller-based status bar appearance = NO 
Status bar style = Opaque black style