Ios 更改状态栏颜色目标-C
在我的应用程序中,有一个暗模式,我想知道如何更改状态栏的颜色? 谢谢Ios 更改状态栏颜色目标-C,ios,objective-c,xcode,Ios,Objective C,Xcode,在我的应用程序中,有一个暗模式,我想知道如何更改状态栏的颜色? 谢谢 可能位于didfishlaunchwithoptions中的代码行下方AppDelegate.m中的方法可以帮助: [[UIApplication sharedApplication] setStatusBarHidden:NO]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; UIView *statu
可能位于
didfishlaunchwithoptions中的代码行下方AppDelegate.m中的方法可以帮助:
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor whiteColor];//set whatever color you like
}
Swift 3解决方案:
在需要非默认状态栏外观的视图控制器中,使用以下命令:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
一种简单有效的方法是向info.plist
添加属性,并将状态栏样式设置为UIStatusBarStyleLightContent
!
我们应该做的是:
- 首先打开应用程序的
info.plist
文件,然后添加属性UIViewControllerBasedStatusBarAppearance
,并将其值设置为NO
- 然后转到
Appdelegate
或任何类文件,并使用以下代码:
UIApplication.sharedApplication.statusBarStyle = UIStatusBarStyleLightContent;
编写代码的另一种方法是:
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleLightContent];
好,根据setStatusBarStyle:animated:
自iOS 9.0以来已被弃用,您现在应该将此代码改为:
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
祝你好运,编码快乐!:) 这就是我的工作
单击导航栏->样式->默认值
然后选择半透明,并在该条的下方选择淡色->(选择您想要的任何颜色)在plist中添加这些线条
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
UIStatusBarStyle
UIStatusBarStyleLightContent
UIViewControllerBasedStatusBarAppearance
自从iOS 13问世以来,我们不得不求助于状态栏中的新属性,将以下代码置于
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if (@available(iOS 13.0, *)) {
UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor whiteColor];
}
[[UIApplication sharedApplication].keyWindow addSubview:statusBar];
} else {
UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor whiteColor];
}
}
这解决了iOS 13中发生的崩溃问题,也为其他iOS版本设置了框架不要这样做,这是一个黑客行为,可能会因为使用私有API而拒绝你的应用。谢谢!只有这样才能遮住条形图的状态。只需更改statusBar.alpha'setStatusBarStyle:'已弃用:在iOS 9.0中首次弃用-使用-[UIViewController preferredStatusBarStyle]-(UIStatusBarStyle)preferredStatusBarStyle{返回UIStatusBarStyleLightContent;}。下面是针对目标C的最新解决方案。这对我来说很有效,这应该是可以接受的答案。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if (@available(iOS 13.0, *)) {
UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor whiteColor];
}
[[UIApplication sharedApplication].keyWindow addSubview:statusBar];
} else {
UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = [UIColor whiteColor];
}
}