`

Customizing Navigation Bar and Status Bar in iOS 7

    博客分类:
  • ios
 
阅读更多

Like many of you, I have been very busy upgrading my apps to make them fit for iOS 7. The latest version of iOS introduces lots of visual changes. From a developer’s perspective, the navigation bar and status bar are two noticeable changes that need to cater. The status bar is now transparent, that means the navigation bar behind it shows through. In some cases, the background image for a navigation bar can extend up behind the status bar.

Some time ago, I’ve written a tutorial about how to customize a navigation bar. I think it’s time to revisit the customization and see how it is done in iOS 7. Here are some of the tips and tricks that you’ll find in this article:

  • Setting the background color of navigation bar
  • Using background image in navigation bar
  • Customizing the color of back button
  • Changing the font of navigation bar title
  • Adding multiple bar button items
  • Changing the style of status bar
  • Hiding the status bar
custom navigation bar iOS 7

You’ll need Xcode 5 to properly execute the code as presented in this tutorial. So if you’re still using older versions of Xcode, make sure you upgrade to Xcode 5 before running the sample Xcode project.

Default Navigation Bar in iOS 7

Before we go in to the customization, let’s first take a look at the default navigation bar generated by Xcode 5 and iOS 7. Simply create a Xcode project using Single View Controller template. Embed the view controller in a navigation controller. If you don’t want to start from scratch, you can justdownload this sample Xcode project.

Xcode 5 bundles both iOS 6 and iOS 7 Simulators. Try to run the sample project using both versions of Simulators.

Default Navigation Bar

As you can see, the navigation bar in iOS 7 is by default intertwined with the status bar. The default color is also changed to light gray, as well.

Changing the Background Color of Navigation Bar

In iOS 7, the tintColor property is no longer used for setting the color of the bar. Instead, use the barTintColor property to change the background color. You can insert the below code in the didFinishLaunchingWithOptions: of AppDelegate.m.

1
[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];

Here is the result:

Change Navigation Bar Background Color

Normally you want to use your own color as the system color doesn’t look nice. Here is a very useful macro for setting RGB color:

1
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

Simply put it somewhere at the beginning of AppDelegate.m and use it to create any UIColor object with whatever RGB color you want. Below is an example:

1
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];

By default, the translucent property of navigation bar is set to YES. Additionally, there is a system blur applied to all navigation bars. Under this setting, iOS 7 tends to desaturate the color of the bar. Here are the sample navigation bars with different translucent setting.

Navigation Bar Translucent

To disable the translucent property, you can simply select the navigation bar in Storyboard. Under Attribute Inspectors, uncheck the translucent checkbox.

Storyboard Navigation Bar Translucent

Using Background Image in Navigation Bar

If your app uses a custom image as the background of the bar, you’ll need to provide a “taller” image so that it extends up behind the status bar. The height of navigation bar is changed from 44 points (88 pixels) to 64 points (128 pixels).

You can still use the setBackgroundImage: method to assign a custom image for the navigation bar. Here is the line of code for setting the background image:

1
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];

The sample Xcode project bundles two different background images: nav_bg.png and nav_bg_ios7.png. Try to test them out.

Navigation Bar Background Image

Changing the Font of Navigation Bar Title

Just like iOS 6, you can customize the text style by using the “titleTextAttributes” properties of the navigation bar. You can specify the font, text color, text shadow color, and text shadow offset for the title in the text attributes dictionary, using the following text attribute keys:

  • UITextAttributeFont – Key to the font
  • UITextAttributeTextColor – Key to the text color
  • UITextAttributeTextShadowColor – Key to the text shadow color
  • UITextAttributeTextShadowOffset – Key to the offset used for the text shadow

Here is the sample code snippets for altering the font style of the navigation bar title:

1
2
3
4
5
6
7
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
    shadow.shadowOffset = CGSizeMake(01);
    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                           shadow, NSShadowAttributeName,
                                                           [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];

If you apply the change to the sample app, the title of navigation bar should look like this:

Custom Navigation Bar Title

Customizing the Color of Back button

In iOS 7, all bar buttons are borderless. The back button is now a chevron plus the title of the previous screen (or just displays ‘Back’ as the button title if the title of the previous screen is nil). To tint the back button, you can alter the tintColor property, which provides a quick and simple way to skin your app with a custom color. Below is a sample code snippet:

1
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

In addition to the back button, please note that the tintColor property affects all button titles, and button images.

Custom Back Button Color

If you want to use a custom image to replace the default chevron, you can set the backIndicatorImage and backIndicatorTransitionMaskImage to your image.

1
2
    [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back_btn.png"]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back_btn.png"]];

The color of the image is controlled by the tintColor property.

Chevron replacement iOS 7

Use Image as Navigation Bar Title

Don’t want to display the title of navigation bar as plain text? You can replace it with an image or a logo by using a line of code:

1
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appcoda-logo.png"]];

We simply change the titleView property and assign it with a custom image. This is not a new feature in iOS 7. The code also applies to lower versions of iOS.

Custom Navigation Title Image

Adding Multiple Bar Button Items

Again, this tip is not specifically for iOS 7. But as some of you have raised such question before, I decide to put the tip in this tutorial. From time to time, you want to add more than one bar button item on one side of the navigation bar. Both the leftBarButtonItems and rightBarButtonItems properties lets you assign custom bar button items on the left/right side of the navigation bar. Say, you want to add a camera and a share button on the right side of the bar. You can use the following code:

1
2
3
4
5
    UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
    UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
    
    NSArray *actionButtonItems = @[shareItem, cameraItem];
    self.navigationItem.rightBarButtonItems = actionButtonItems;

Here is the sample result:

Add Multiple Bar Button Items

Changing the Style of Status Bar

In older versions of iOS, the status bar was always in black style and there is not much you can change. With the release of iOS 7, you’re allowed to change the appearance of the status bar per view controller. You can use a UIStatusBarStyle constant to specify whether the status bar content should be dark or light content. By default, the status bar displays dark content. In other words, items such as time, battery indicator and Wi-Fi signal are displayed in dark color. If you’re using a dark background in navigation bar, you’ll end up with something like this:

Dark Navigation Bar

In this case, you probably need to change the style of status bar from dark to light. There are two ways to do this. In iOS 7, you can control the style of the status bar from an individual view controller by overriding the preferredStatusBarStyle:

1
2
3
4
-(UIStatusBarStyle)preferredStatusBarStyle 
{ 
    return UIStatusBarStyleLightContent; 
}

For the sample app, simply put the above code in the RecipeNavigationController.m and the status bar will display light content.

Dark Navigation Bar Light Status Bar

The method introduced above is the preferred way to change the status bar style in iOS 7. Alternatively, you can set the status bar style by using the UIApplication statusBarStyle method. But first you’ll need to opt out the “View controller-based status bar appearance”. Under the Info tab of the project target, insert a new key named “View controller-based status bar appearance” and set the value to NO.

View Controller-based Status Bar

By disabling the “View controller-based status bar appearance”, you can set the status bar style by using the following code:

1
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

Hiding the Status Bar

In any case you want to hide the status bar, you can override the prefersStatusBarHidden: in your controller:

1
2
3
4
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

Summary

iOS 7 presents developers with new freedom to customize the appearance of navigation bar and status bar. If you’re porting the app from iOS 6 to iOS 7 or creating a brand-new app for iOS 7, I hope you’ll find these tips useful.

For your complete reference, you can download the source code of the demo project from here. Just uncomment any code snippets in the sample project to test out the change.

Like many of you, I’m still exploring all the new changes of iOS 7 SDK. I am by no means an expert on iOS 7. If you find any errors in the article, please do let me know. If you find any tips and tricks related to navigation bar and status bar, please also share with us by leaving comment below.

 

from:http://www.appcoda.com/customize-navigation-status-bar-ios-7/

分享到:
评论

相关推荐

    实训商业源码-付费进群自动定位版本-毕业设计.zip

    实训商业源码-付费进群自动定位版本-毕业设计.zip

    单级热电制冷器件,全球前20强生产商排名及市场份额(by QYResearch).pdf

    单级热电制冷器件,全球前20强生产商排名及市场份额(by QYResearch).pdf

    实训商业源码-Turbo Website Reviewer SEO分析报告工具源码-毕业设计.zip

    实训商业源码-Turbo Website Reviewer SEO分析报告工具源码-毕业设计.zip

    COMSOL模拟铌酸锂波导倍频PPLN技术解析及实操经验分享

    内容概要:本文详细介绍了利用COMSOL进行铌酸锂波导倍频(PPLN)仿真的方法和技术难点。首先讨论了材料设置中非线性系数d33的空间调制方式,推荐使用tanh函数代替sign函数以提高收敛性。接着阐述了波导结构的选择和模式分析的关键步骤,强调了正确设置边界条件的重要性。对于网格划分提出了在极化周期交界处局部加密的方法,并解释了分步求解策略以节省内存。最后,作者提醒注意相位匹配条件以及考虑实际器件制造中的工艺误差对转换效率的影响。 适合人群:从事非线性光学研究、光子学器件设计的研究人员和工程师。 使用场景及目标:帮助读者掌握COMSOL软件中针对PPLN结构的仿真技巧,优化仿真流程,提升仿真准确性,解决实际项目中可能遇到的问题。 阅读建议:由于文中涉及大量具体的操作细节和技术要点,建议读者结合自己的项目背景仔细研读每个部分的内容,并尝试将所学应用到实践中去。

    PLOT2222222222

    PLOT2222222222

    新能源领域的技术创新:19永磁直驱风机+混合储能+PQ逆变并网系统

    内容概要:本文介绍了“19永磁直驱风机+混合储能+PQ逆变并网”系统,这是一种集成永磁直驱风机、混合储能设备和PQ逆变器的综合性解决方案,旨在实现可再生能源的高效利用和电网的稳定并网。文中详细阐述了各组件的工作原理及其协同效应,强调了该系统在提高能量转换效率、增强电网稳定性和改善供电质量方面的优势。通过对实际应用效果的分析,展示了该系统在低风速环境下的稳定输出能力、混合储能系统的削峰填谷作用以及PQ逆变器的智能调控和保护功能。 适合人群:从事新能源研究和技术开发的专业人士,关注绿色能源发展的科研工作者和政策制定者。 使用场景及目标:适用于风电场建设、分布式能源系统规划等领域,旨在推动可再生能源的广泛应用,促进电网的智能化和稳定性。 其他说明:随着可再生能源的发展,该系统有望在全球范围内获得更广泛的应用,成为未来能源领域的重要组成部分。

    商用车P2并联混合动力系统HCU控制策略解析与建模指南

    内容概要:本文详细介绍了商用车P2并联混合动力系统的HCU(整车控制器)控制策略及其建模方法。首先探讨了模式切换策略,针对不同工况如车辆速度、电池电量等因素进行模式选择。接着深入讲解了扭矩分配策略,考虑到了温度变化以及坡道情况对扭矩分配的影响。此外,还讨论了能量回收策略,利用预测性制动提高能量利用率。最后提及了故障降级策略,确保系统在出现故障时能够快速响应。文中提供了多个具体代码片段来辅助理解和实施这些策略。 适合人群:从事汽车电子控制系统开发的技术人员,尤其是专注于混合动力系统的研究人员和工程师。 使用场景及目标:帮助开发者将理论性的功能规范转化为实际可用的控制模型,适用于商用车P2并联混合动力系统的开发过程中,旨在提升系统的效率和平顺性。 其他说明:建议读者在实践中不断调整和完善模型参数,以适应不同的应用场景和技术要求。同时,在构建模型时应注意保持良好的可追溯性和验证性,以便后续维护和改进。

    openai-agents-python-AI人工智能资源

    OpenAI Agents SDK

    干式无油螺杆空压机,2024年前13大企业占据全球78%的市场份额.pdf

    干式无油螺杆空压机,2024年前13大企业占据全球78%的市场份额.pdf

    实训商业源码-多功能水果外卖电子商务手机模板-毕业设计.zip

    实训商业源码-多功能水果外卖电子商务手机模板-毕业设计.zip

    .NET Framework 3.5(Windows server系统)

    .NET Framework 3.5(Windows server系统)

    全国大学生电子设计竞赛的预测.pdf

    电子设计竞赛相关资源

    电子设计竞赛论文要点.pdf

    电子设计竞赛相关资源

    膜用聚砜,全球前9强生产商排名及市场份额(by QYResearch).pdf

    膜用聚砜,全球前9强生产商排名及市场份额(by QYResearch).pdf

    信號完整性小技巧 #2 EYE CONTOUR.pdf

    信號完整性小技巧 #2 EYE CONTOUR.pdf

    常规S参数与共模差模S参数转换.pdf

    常规S参数与共模差模S参数转换

    实训商业源码-UNIAPP开发的软件市场多端源码-毕业设计.zip

    实训商业源码-UNIAPP开发的软件市场多端源码-毕业设计.zip

    振动与噪声分析领域中LMS系统的模态分析与锤击实验实践指南

    内容概要:本文详细介绍了LMS(LabVIEW Multifunctional System)测试系统在模态分析和锤击实验中的应用。首先解释了LMS系统及其核心功能——模态分析,这是一种用于确定结构振动特性的关键技术,可以获取固有频率、阻尼比和模态形状等参数。接着阐述了锤击实验的具体步骤,包括实验准备、数据采集、激励与响应记录、数据分析和结果解读。文中还简要介绍了LMS系统中使用的软件工具,涵盖数据导入、滤波与去噪、频域分析、模态识别与提取等功能。最后强调了模态分析和锤击实验在结构设计、优化和故障诊断中的重要作用。 适合人群:从事机械工程、振动与噪声分析的技术人员,尤其是需要掌握模态分析和锤击实验的应用工程师。 使用场景及目标:适用于希望深入了解LMS系统在模态分析和锤击实验中的具体应用,提升结构设计和故障诊断能力的专业人士。目标是在实际工作中更好地利用LMS系统进行振动与噪声分析。 其他说明:本文不仅提供了理论知识,还详细描述了实验操作流程,帮助读者更好地理解和应用相关技术。

    MATLAB 2021b深度学习入门:手写数字图像识别与CNN特征提取

    内容概要:本文详细介绍了使用MATLAB 2021b进行手写数字图像识别的深度学习入门教程。主要内容涵盖从加载MNIST数据集到构建并训练卷积神经网络(CNN)的全过程。首先展示了如何读取和预览数据集,接着逐步讲解了CNN各层的设计思路及其功能,如卷积层、批规范化层、ReLU激活函数以及池化层的作用。随后,演示了如何将数据集划分为训练集和验证集,并设置训练选项来启动模型训练。此外,还提供了提取和展示中间层特征图的方法,帮助理解CNN的工作机制。最后,评估了模型性能并通过混淆矩阵直观地展示了分类效果。 适合人群:初学者和希望快速掌握MATLAB环境下深度学习应用的研究人员或学生。 使用场景及目标:适用于希望通过实际操作加深对深度学习理论和技术的理解,特别是对于想要了解CNN架构及其在图像识别任务中具体应用的人群。 其他说明:文中提供的代码片段可以直接在MATLAB环境中执行,便于读者跟随教程动手实践。同时,强调了模型训练过程中需要注意的问题,如过拟合现象的监控等。

    博世汽车电驱仿真模型:同步与异步电机的FOC控制及优化技术

    内容概要:本文详细介绍了博世汽车电驱仿真模型的技术细节,涵盖同步电机和异步电机的相电流波形优化、自动弱磁FOC控制、正反转切换电流稳定性和铁损计算等方面。文中展示了MATLAB和C语言编写的自动化控制脚本,以及Python编写的高效铁损计算方法。特别强调了在高转速条件下保持电流波形的稳定性,以及通过动态磁滞模型提高铁损计算精度。 适合人群:从事电动汽车驱动系统研究的专业人士、电机控制系统工程师和技术研究人员。 使用场景及目标:适用于需要深入了解和应用先进电机控制技术和仿真的场合,如电动汽车动力系统的开发和测试。目标是掌握先进的FOC控制算法和优化技术,提高电机性能和效率。 其他说明:文章不仅提供了理论背景,还包括具体的实现代码片段,便于读者理解和实际操作。

Global site tag (gtag.js) - Google Analytics