iOS开发基础之设置状态栏和二维码的unspported type found 问题

最近遇到设置状态栏和二维码AVCaptureMetadataOutput setMetadataObjectTypes: unspported type found的错误问题,所以我在这里进行总结一下,方便大家以后的使用;

设置状态栏:

(个人方法:是在AppDelegate头文件中设置,自己的主题色)

[[UINavigationBar appearance] setBarTintColor:THEME_COLOR];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

具体讲解:

状态栏的字体为黑色:UIStatusBarStyleDefault

状态栏的字体为白色:UIStatusBarStyleLightContent

一、在info.plist中,将View controller-based status bar appearance设为NO

状态栏字体的颜色只由下面的属性设定,默认为白色:

// default is UIStatusBarStyleDefault

[UIApplication sharedApplication].statusBarStyle

解决个别vc中状态栏字体颜色不同的办法

1、在info.plist中,将View controller-based status bar appearance设为NO.

2、在app delegate中:

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

3、在个别状态栏字体颜色不一样的vc中

-(void)viewWillAppear:(BOOL)animated{

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;}

-(void)viewWillDisappear:(BOOL)animated{

[super viewWillDisappear:animated];

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;}

二、在info.plist中,将View controller-based status bar appearance设为YES,或者没有设置。

View controller-based status bar appearance的默认值就是YES。

如果View controller-based status bar appearance为YES。

则[UIApplication sharedApplication].statusBarStyle 无效。

用下面的方法:

1、在vc中重写vc的preferredStatusBarStyle方法。

-(UIStatusBarStyle)preferredStatusBarStyle{

return UIStatusBarStyleDefault;}

2、在viewDidload中调用:[self setNeedsStatusBarAppearanceUpdate];

但是,当vc在nav中时,上面方法没用,vc中的preferredStatusBarStyle方法根本不用被调用。

原因是,[self setNeedsStatusBarAppearanceUpdate]发出后,

只会调用navigation controller中的preferredStatusBarStyle方法,

vc中的preferredStatusBarStyley方法跟本不会被调用。

解决办法有两个:

方法一:

设置navbar的barStyle 属性会影响status bar 的字体和背景色。如下。

//status bar的字体为白色

//导航栏的背景色是黑色。

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

//status bar的字体为黑色

//导航栏的背景色是白色,状态栏的背景色也是白色。

//self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

方法二:

自定义一个nav bar的子类,在这个子类中重写preferredStatusBarStyle方法:

MyNav* nav = [[MyNav alloc] initWithRootViewController:vc];

self.window.rootViewController = nav;

@implementation MyNav

- (UIStatusBarStyle)preferredStatusBarStyle{

UIViewController* topVC = self.topViewController;

return [topVC preferredStatusBarStyle];}

二维码AVCaptureMetadataOutput setMetadataObjectTypes: unspported type found的错误问题

我使用的是QR代码扫描,当你调用扫描时,会询问你打开相机的权限,如果你点击允许,那么下次点击时可以直接调用相机扫码,但是如果你点击的是不允许,然后再次点击时会Crash掉,这是因为output.availableMetadataObjectTypes被设置为空了,相当于把相机的权限关掉了,再次点击时,不进行判断相机的权限是无法调用的,否则就会Crash,所以要在调用二维码扫码之前进行权限判断,添加一下代码,记得添加#import <AVFoundation/AVFoundation.h>库:

//必须添加这个,否则如果用户选择不允许打开相机时,当再次调用相机,会崩溃
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请先打开APP相机访问权限" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
        return;
    }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hbblzjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值