iOS tabbar设置

1. 深层定制 tabBar. 通过打印子视图,我们发现 tabBar 的层级结构变化非常大.

在 iOS10中可以对 tabBarItem 里的内容进行定制.比如 badge 的颜色和文字的属性,item 的 TintColor

1.1>改变 tabBar 的unselectedItemTintColor

    self.tabBar.tintColor = [UIColor colorWithRed:(128 / 255.0) green:(177 / 255.0) blue:(34 / 255.0) alpha:1];
    self.tabBar.unselectedItemTintColor = [UIColor redColor];

设置unselectedItemTintColor

在10之前我们设置方式

+ (void)initialize {
    // 未选中时
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    textAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];

    // 选中时字体颜色和选中图片颜色一致
    NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
    selectedTextAttrs[NSFontAttributeName] = textAttrs[NSFontAttributeName];
    selectedTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:(128 / 255.0) green:(177 / 255.0) blue:(34 / 255.0) alpha:1];

    // 通过appearance统一设置所有UITabBarItem的文字属性样式
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
}

1.2>设置 badge 的颜色

10之前 badge 的背景默认是红色

 self.tabBarItem.badgeColor = [UIColor blueColor];
    self.tabBarItem.badgeValue = @"100";

    NSDictionary *dict = @{
                           NSForegroundColorAttributeName : [UIColor redColor],
                           NSFontAttributeName : [UIFont systemFontOfSize:10]
                           };
    [self.tabBarItem setBadgeTextAttributes:dict forState:UIControlStateNormal];

设置 badge

2. 动态字体大小(Dynamic Type)

可以设置字体随系统变化动态调整大小

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
    [self.view addSubview:label];
    label.text = @"字体随系统字体变化";

    // 1. 设置 font 大小
    label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    // 2. 允许调整大小
    label.adjustsFontForContentSizeCategory = YES;

设置之后 当我们调整系统字体大小, label 的字体也会随着进行调整.

iOS tabbar设置_第1张图片
调整系统字体
iOS tabbar设置_第2张图片
label 的字体
iOS tabbar设置_第3张图片
调系统字体
iOS tabbar设置_第4张图片
label 显示大小

3. 刷新控件(UIRefreshControl)

在 iOS10之后,只要继承UIScrollView,都具有刷新的功能

  UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 200, 400, 500)];
    scrollView.backgroundColor = [UIColor redColor];
    scrollView.contentSize = CGSizeMake(500, 1000);
    [self.view addSubview:scrollView];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
    [scrollView addSubview:imageView];

    // 添加下拉刷新控件
    UIRefreshControl *ref = [[UIRefreshControl alloc] init];
    // 监听刷新方法
    [ref addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    scrollView.refreshControl = ref;
- (void)refresh:(UIRefreshControl *)ref {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 结束刷新
       [ref endRefreshing];
    });
}

4. 语音识别

iOS10开放了语音识别的 api,
1.需要导入框架

#import <Speech/Speech.h>

2.在 info.plist 进行配置,添加key 值

Privacy - Speech Recognition Usage Description

3.创建实例对象

 SFSpeechRecognizer *recognizer = [[SFSpeechRecognizer alloc] initWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"录音.MP3" withExtension:nil];
    SFSpeechURLRecognitionRequest *request = [[SFSpeechURLRecognitionRequest alloc] initWithURL:url];

    [recognizer recognitionTaskWithRequest:request resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", error.userInfo);
        } else {
            NSLog(@"%@", result.bestTranscription.formattedString);
        }

    }];

你可能感兴趣的:(iOS tabbar设置)