iOS彻底隐藏app状态栏StatusBar的方法汇总

10个月前 (01-03) fancyang IOS相关开发 0评论 已收录 683℃
方法一:
整个app中隐藏
1.首先工程的.plist文件中View controller-based status bar appearance这一栏必须设置为YES
2.再修改以下代码
//For Objective-C:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [application setStatusBarHidden:YES];

  return YES;
}
//For Swift:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
  application.statusBarHidden = true

  return true
}
方法二:
在单个视图隐藏:
1.首先工程的.plist文件中View controller-based status bar appearance这一栏设置为NO;
Status bar is initially hidden设置为YES。
2.再添加以下代码
//For Objective-C:

- (BOOL)prefersStatusBarHidden {
  return YES;
}
//For Swift:

override func prefersStatusBarHidden() -> Bool {
  return true
}

方法三:
在某一特定方法隐藏:
1.首先工程的.plist文件中View controller-based status bar appearance这一栏必须设置为YES
2.再添加以下代码: 
//For Objective-C:

- (void)HideStataBar{
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        //调用隐藏方法
        [self prefersStatusBarHidden];
        
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
}



//实现隐藏方法
- (BOOL)prefersStatusBarHidden{
    
    return hidestatusbar;
}

//其中hidestatusbar为BOOL型YES或NO,可以在调用方法前自定义。

//For Swift:

func hideStataBar() {
    if responds(to: #selector(self.setNeedsStatusBarAppearanceUpdate)) {
 //调用隐藏方法
        prefersStatusBarHidden
        perform(#selector(self.setNeedsStatusBarAppearanceUpdate))
    }
}

//实现隐藏方法
var prefersStatusBarHidden: Bool {
    return hidestatusbar
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//method 1:
//Hidden in the whole app:
//Firstly, View controller-based status bar appearance in the .plist file must be set to YES.

//If you want status bar to be hidden in the whole app:
//For Objective-C:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [application setStatusBarHidden:YES];

  return YES;
}
//For Swift:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
  application.statusBarHidden = true

  return true
}


//method 2:
//Hidden in Specify View Controller
//If you want status bar is disappeared in Specify View Controller, in .m file, just implement:
//For Objective-C:

- (BOOL)prefersStatusBarHidden {
  return YES;
}
//For Swift:

override func prefersStatusBarHidden() -> Bool {
  return true
}


//method 3:

//Hidden in Specify method:
//Firstly, View controller-based status bar appearance in the .plist file must be set to YES.

//2.add the below code:
//For Objective-C:
- (void)HideStataBar{

    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {

        //Call the Hidden Method

        [self prefersStatusBarHidden];

        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];

    }

}

//Implement Hidden Method

- (BOOL)prefersStatusBarHidden{

    return hidestatusbar;

}

//the hidestatusbar is a BOOL type YES or NO,you can customize it before you call the Hidden method.

For Swift:

func hideStataBar() {
    if responds(to: #selector(self.setNeedsStatusBarAppearanceUpdate)) {
        //Call the Hidden Method
        prefersStatusBarHidden
        perform(#selector(self.setNeedsStatusBarAppearanceUpdate))
    }
}

//Implement Hidden Method
var prefersStatusBarHidden: Bool {
    return hidestatusbar
}
博主

这货来去如风,什么鬼都没留下!!!

相关推荐

嗨、骚年、快来消灭0回复。

000113;您的ip地址是54.224.247.42; 当前围观人数:2

隐私政策

橘花2支持在线更新了,点我下更新支持文档然后托给橘花,再点SSP面板右键更新