我有一些视图控制器,顶部有一个工具栏,如下所示

如何填充状态栏背景以匹配工具栏背景,从而使其与新的iOS 7样式匹配?
发布于 2013-12-22 15:34:53
您需要在app委托中添加一个子视图,并根据您的喜好更改颜色。下面是位于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;
}看一看评论。您可以在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];edit1:
如果试图在导航控制器中的nag栏顶部添加子视图,则必须将子视图的位置提高一倍,如下所示:
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];此外,还需要添加导航控制器导航栏的子视图。我将子视图的背景色设置为紫色,但您可以更改它。
https://stackoverflow.com/questions/20728970
复制相似问题