Allen Woody

如果你像程序员一样工作,你就是程序员。如果你像架构师一样工作,你就是架构师。

嗨,我是朱洪伍 (@hnxyzhw),一名 iOS 开发者。现居北京,就职于中化能源科技有限公司。正在get新技能,探求创意之源。


我在简书上创建的有关于iOS开发的技术专辑,欢迎各位前来了解!

#文章内容

适配iOS状态栏颜色

Xcode 升级后,旧的状态栏的样式设置方式会引起警告错误:

<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable
<Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

具体原因:设置 app 的状态栏样式的使用了旧的方法。 之前需要兼容iOS7,iOS8。会在 info.plist 里面的 View controller-based status bar appearance (默认会为 YES)设置为NO,如果不设置的话默认也是YES。

可以调用该方法来改变状态栏目的颜色: [UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

但是到了iOS9就会报错警告,虽然不影响程序运行,但作为强迫症的程序猿,这个警告的存在不能忍啊。为了兼容iOS9,需要做以下修改: 将info.plist 里面的 View controller-based status bar appearance 删除(默认为 YES),或设置为YES。同时需要使用新的方法来设置状态栏目的颜色:

- (UIStatusBarStyle)preferredStatusBarStyle;
- (UIViewController *)childViewControllerForStatusBarStyle;
- (void)setNeedsStatusBarAppearanceUpdate

1:如果我们的rootViewController是ViewController那么只需要在VC中实现以下方法(clean 下或者删除应用程序重新运行):

//设置状态栏的白色
 -(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

2:如果我们的rootViewController是navigationController,按照上边的方法设置你会发现是无法解决问题的。 我们必须设置 rootViewController,编译器才会去 rootViewController 中重载 preferredStatusBarStyle 方法。此时我们的 rootViewController是当前的navigationController 在appdelegate里面我们对rootViewController做一下设置:

self.window.rootViewController = self.navigationController;

在对应的VC里我们需要做以下设置:

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

对于navigationController状态栏颜色的修改,我们也可以重新写一个navigationController的分类来实现preferredStatusBarStyle方法的调用。

//.h文件
//  UINavigationController+StatusBarStyle.h
#import
 
@interface UINavigationController (StatusBarStyle)
 
@end

// .m文件
//  UINavigationController+StatusBarStyle.m
#import "UINavigationController+StatusBarStyle.h"
 
@implementation UINavigationController (StatusBarStyle)
 
- (UIStatusBarStyle)preferredStatusBarStyle
{
    //also you may add any fancy condition-based code here
    return UIStatusBarStyleLightContent;
}
 
@end

通过这个分类就可以自定义我们的状态栏的颜色了。

3:还有就是在初始化登录注册界面时,一些button的背景图没有使用切图。通过颜色值来绘制一张纯色的背景图片。在编译运行时出现一下错误:

<Error>: CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

具体绘图的方法:

#pragma mark - uicolor - uiimage
- (UIImage*) createImageWithColor:(UIColor*) color rect:(CGRect)rect{
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

根据返回的错误问题,可以认为是在初始化的时候有问题。检索了一下类似的问题,得到如下回复:

**若使用uikit绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate 的ref并且不能用于画图。
使用UiKit,你只能在当前上下文中绘图,所以如果你当前处于UIGraphicsBeginImageContextWithOptions函数或drawRect:方法中,你就可以直接使用UIKit提供的方法进行绘图。
如果你持有一个context:参数,那么使用UIKit提供的方法之前,必须将该上下文参数转化为当前上下文。
幸运的是,调用UIGraphicsPushContext 函数可以方便的将context:参数转化为当前上下文,记住最后别忘了调用UIGraphicsPopContext函数恢复上下文环境。**
**使用Core Graphics之前需要指定一个用于绘图的图形上下文(CGContextRef),这个图形上下文会在每个绘图函数中都会被用到。
如果你持有一个图形上下文context:参数,那么你等同于有了一个图形上下文,这个上下文也许就是你需要用来绘图的那个。
如果你当前处于UIGraphicsBeginImageContextWithOptions函数或drawRect:方法中,并没有引用一个上下文。为了使用Core Graphics,你可以调用UIGraphicsGetCurrentContext函数获得当前的图形上下文。**

最后在登陆注册界面,按键的背景色使用了切图。其他地方按键的背景色使用了绘制的方法,统一主题色调,方便修改。

上一篇文章

app审核中遇到的问题03(涉及到金融类的需要用企业级别的账号申请上架)

2017年2月17日 上午2:54发件人 Apple • 2. 3 PERFORMANCE: ACCURATE METADATA • 0. 10.0 BEFORE YOU SUBMIT: PROGRAM LICENSE AGREEMENTPerformance - 2.3.7Your app name to be displayed on the App Store includes keywords or descriptors, which are not appropriate f...…

iOS,AppleStore继续阅读
下一篇文章

app审核中遇到的问题02(应用内虚拟金币购买应用内商品被拒)

2016年11月24日 上午11:50发件人 Apple • 3. 1.1 BUSINESS: PAYMENTS - IN-APP PURCHASE Business - 3.1.1We noticed that your app enables the purchase of content, services, or functionality in the app by means other than the In-App Purchase API, which is not al...…

iOS,AppleStore继续阅读