首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UIImagePickerController的中文界面和改变statusBar的颜色

UIImagePickerController的中文界面和改变statusBar的颜色

作者头像
honey缘木鱼
发布2018-06-04 14:59:07
1.8K0
发布2018-06-04 14:59:07
举报
文章被收录于专栏:娱乐心理测试娱乐心理测试

用户选择头像功能是最常见的调用相机相册场景,调用系统的方法会存在两个问题:1.除了UIImagePickerController的拍照页面,UISearchBar的取消按钮,键盘上的返回、完成等按钮,以及其他系统界面中带有英文的,2.很多时候我们App 的状态栏设计格式和选择照片页面格式不符合的问题。

  1. 如何变为中文界面?

在info.plist中添加Localized resources can be mixed value值为YES 如下图:

图片

2.改变statusBar的颜色 首先定义

@interface NewProjectViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property(strong,nonatomic)UIImagePickerController*pickerViewController;
@end

在点击选择头像按钮代码:

UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"请选择图片来源" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //从照相机拍照
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            self.pickerViewController = [[UIImagePickerController alloc] init];
            self.pickerViewController.delegate = self;
            self.pickerViewController.allowsEditing = YES;
            self.pickerViewController.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:self.pickerViewController animated:YES completion:nil];
        }else{
            NSLog(@"哎呀,没有摄像头");
        }
    }];
    //从手机相册选取
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
            self.pickerViewController = [[UIImagePickerController alloc]init];
            self.pickerViewController.delegate = self;
            self.pickerViewController.allowsEditing = YES;//是否可以对原图进行编辑
            
            self.pickerViewController.navigationController.navigationBar.translucent = NO;
            self.pickerViewController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
            self.pickerViewController.navigationBar.barTintColor = [UIColor colorWithRed:20.f/255.0 green:24.0/255.0 blue:38.0/255.0 alpha:1];
            self.pickerViewController.navigationBar.tintColor = [UIColor whiteColor];
            //    更改titieview的字体颜色
            NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
            attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
            [self.pickerViewController.navigationBar setTitleTextAttributes:attrs];
            
            self.pickerViewController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:self.pickerViewController animated:YES completion:nil];
        }
        else{
            NSLog(@"图片库不可用");
        }
    }];
    //取消
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertC addAction:cameraAction];
    [alertC addAction:photoAction];
    [alertC addAction:cancelAction];
    [self presentViewController:alertC animated:YES completion:nil];

其中设置改变导航栏颜色的代码为:

self.pickerViewController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
            self.pickerViewController.navigationBar.barTintColor = [UIColor colorWithRed:20.f/255.0 green:24.0/255.0 blue:38.0/255.0 alpha:1];
            self.pickerViewController.navigationBar.tintColor = [UIColor whiteColor];
            //    更改titieview的字体颜色
            NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
            attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
            [self.pickerViewController.navigationBar setTitleTextAttributes:attrs];

改变状态栏的颜色方法为:

方法1:

声明一个类继承UIImagePickerController

#import <UIKit/UIKit.h>
@interface ImagePickerController : UIImagePickerController
@end

再其.m文件实现以下方法:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

方法2:

实现UIImagePickerController的延展,并实现preferredStatusBarStyle方法

#import "UIImagePickerController+util.h"

@implementation UIImagePickerController (util)

// 状态栏设置
- (UIStatusBarStyle)preferredStatusBarStyle {

    return UIStatusBarStyleLightContent;
}
@end

由于很多大神分享UIImagePickerController的具体使用方法,在这里不做多余的阐述了,有问题可以一起讨论。

下次想写些阿里云上传图片的方法。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.06.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档