弹出相册 Demo

2017-06-01  本文已影响18人  小苗晓雪

根据提示需要配置 key 值,因为访问了用户的私有空间
debug 区域会出现如下的警告提示:
This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryUsageDescription key with a string value explaining to the user how the app uses this data.
只需要在 info.plist 文件中加入 NSPhotoLibraryUsageDescription即可, value 值写一写提示即可,如:即将访问您的相册,是否允许...巴拉巴拉~

效果图 Gif

效果图 Gif.gif

UITabBarControllerDelegate代理弹出相册

#import "TabBarViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
#import "FifthViewController.h"

@interface TabBarViewController () <UITabBarControllerDelegate>


@end

@implementation TabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
    FourthViewController *fourthVC = [[FourthViewController alloc] init];
    FifthViewController *fifthVC = [[FifthViewController alloc] init];
    self.viewControllers = @[firstVC, secondVC, thirdVC, fourthVC, fifthVC];
    
    /*
    secondVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Item2" image:nil selectedImage:nil];
    thirdVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Item3" image:nil selectedImage:nil];
    fourthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Item4" image:nil selectedImage:nil];
    fifthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Item5" image:nil selectedImage:nil];
*/
    
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"barBack"]];
    self.tabBar.shadowImage = [UIImage imageNamed:@"shadowBorder"];
    self.tabBar.tintColor = [UIColor redColor];
    self.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"tab_indicator"];
    self.tabBar.itemWidth = 60;
    self.tabBar.itemSpacing = 20;

    /*****************************************************/
    self.delegate = self;
    
}


#pragma mark - <UITabBarControllerDelegate>
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0) {
    if ([viewController isKindOfClass:[ThirdViewController class]]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        //相册:
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.allowsEditing = NO;
        [self presentViewController:picker animated:YES completion:nil];
        return NO;
    }
    return YES;
}


- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"%@", viewController);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end

FirstViewController.m文件

#import "FirstViewController.h"

@interface FirstViewController ()



@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImage *normalImage = [[UIImage imageNamed:@"tab_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *selectedImage = [[UIImage imageNamed:@"tab_home_select"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //tintColor控制了选中图片的渲染颜色, 因为默认的 renderMode 是 template 模式:
//    self.tabBarController.tabBar.tintColor = [UIColor blackColor];
    self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"First" image:normalImage selectedImage:selectedImage];
    
    self.tabBarItem.badgeValue = @"10";
    self.tabBarItem.titlePositionAdjustment = UIOffsetMake(-10, -10);
    NSShadow *shadow = [NSShadow new];
    [shadow setShadowOffset:CGSizeMake(10, 10)];
    [shadow setShadowColor:[UIColor colorWithWhite:0.0f alpha:0.75f]];
    [shadow setShadowBlurRadius:5.0f];
    NSDictionary *navBarTitleAttributesString = @{NSForegroundColorAttributeName:[UIColor redColor]};
    [self.tabBarItem setTitleTextAttributes:navBarTitleAttributesString forState:UIControlStateNormal];
    
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

上一篇下一篇

猜你喜欢

热点阅读