상태 표시줄 색상 및 가시성

1855 단어
상태막대의 색상 및 가시성을 제어하는 두 가지 방법이 있습니다.

UIAPplication 사용 방법

  • 전제: Info.plist 파일에 View controller-based status bar appearanceNO로 설정해야 한다.
  • 주의: IOS9은 이미 이 방법을 폐기 방법으로 성명했다.
  • 코드는 다음과 같다
  • - (IBAction)changeColor:(UISegmentedControl *)sender {
        if (sender.selectedSegmentIndex == 0) {
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
        } else {
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
        }
    }
    
    - (IBAction)setHiddenStatus:(UISegmentedControl *)sender {
        if (sender.selectedSegmentIndex == 0) {
            [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
        } else {
            [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
        }
    }
    

    2 UIViewController 사용 방법

  • 전제: Info.plist 파일에 View controller-based status bar appearanceYES로 설정해야 한다.또는 이 키 값을 제거하십시오. 기본값은 YES 입니다.
  • 코드는 다음과 같다
  • - (IBAction)changeColor:(UISegmentedControl *)sender {
        [self setNeedsStatusBarAppearanceUpdate];
    }
    
    - (IBAction)setHiddenStatus:(UISegmentedControl *)sender {
        [self setNeedsStatusBarAppearanceUpdate];
    }
    
    #pragma mark - UIViewController     
    
    - (UIStatusBarStyle)preferredStatusBarStyle
    {
        if (self.colorSegmentControl.selectedSegmentIndex == 0) {
            return UIStatusBarStyleDefault;
        }
        return UIStatusBarStyleLightContent;
    }
    
    - (BOOL)prefersStatusBarHidden
    {
        if (self.hiddenSegmentControl.selectedSegmentIndex == 0) {
            return NO;
        }
        return YES;
    }
    
    //           
    - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
    {
        return UIStatusBarAnimationFade;
    }
    

    좋은 웹페이지 즐겨찾기