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

2018-06-04 14:59:07 浏览数 (2)

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

  1. 如何变为中文界面?

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

图片

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

代码语言:javascript复制
@interface NewProjectViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property(strong,nonatomic)UIImagePickerController*pickerViewController;
@end

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

代码语言:javascript复制
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];

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

代码语言:javascript复制
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

代码语言:javascript复制
#import <UIKit/UIKit.h>
@interface ImagePickerController : UIImagePickerController
@end

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

代码语言:javascript复制
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

方法2:

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

代码语言:javascript复制
#import "UIImagePickerController util.h"

@implementation UIImagePickerController (util)

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

    return UIStatusBarStyleLightContent;
}
@end

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

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

0 人点赞