【发布时间】:2013-12-22 11:37:14
【问题描述】:
我有一些顶部带有工具栏的视图控制器,看起来像这样
如何填充状态栏背景以匹配工具栏背景,使其匹配新的 iOS 7 样式?
【问题讨论】:
标签: objective-c cocoa-touch ios7
我有一些顶部带有工具栏的视图控制器,看起来像这样
如何填充状态栏背景以匹配工具栏背景,使其匹配新的 iOS 7 样式?
【问题讨论】:
标签: objective-c cocoa-touch ios7
您需要在应用程序委托中添加一个子视图并根据自己的喜好更改颜色。这是位于applicationdidfinishLaunchingWithOptions中的代码示例
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Override point for customization after application launch.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
//change this to match your navigation bar or view color or tool bar
//You can also use addStatusBar.backgroundColor = [UIColor BlueColor]; or any other color
addStatusBar.backgroundColor = [UIColor colorWithRed:0.973/255. green:0.973/255. blue:0.973/255. alpha:1];
[self.window.rootViewController.view addSubview:addStatusBar];
}
return YES;
}
看看 cmets。您可以在addStatusBar.backGroundColor 中使用任何类型的颜色来匹配您需要的颜色。请注意,这里的红色只是产生黑色背景,将其更改为您需要的任何颜色。对于深灰色,将其替换为以下代码:
addStatusBar.backgroundColor = [UIColor colorWithRed:85.0/255.0 green:85.0/255.0 blue:85.0/255.0 alpha:1];
编辑:
要更改单个视图中的状态栏颜色,您只需在 ViewDidLoad 方法中插入以下代码(这将访问您要更改的视图)[super viewDidLoad]; 之后应该会更改状态栏的颜色就是你放置代码的那个视图。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:0.973/255. green:0.973/255. blue:0.973/255. alpha:1];
[self.view addSubview:addStatusBar];
编辑1:
如果尝试在导航控制器的导航栏顶部添加子视图,则必须将子视图的位置提高一倍,如下所示:
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, -20, 320, 20);
addStatusBar.backgroundColor = [UIColor colorWithRed:127.0/255. green:0.0/255. blue:127.0/255. alpha:1];
[self.view addSubview:addStatusBar];
[self.navigationController.navigationBar addSubview:addStatusBar];
您还需要将您的子视图添加为导航控制器导航栏的子视图。我将子视图的背景颜色设置为紫色,但您可以更改它。
【讨论】: