[iOS]系统TabBar简单使用

[iOS]系统TabBar简单使用

demo:http://download.csdn.net/download/u012881779/9950684

以前一直使用自定义样式导航和TabBar,最近发觉系统导航和TabBar也蛮好用的,这里demo先简单用用。

UITabBarController

#import "MainViewController.h"
#import "HomeViewController.h"
#import "ShopViewController.h"
#import "BaseTabBar.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 不需要切换动画或分割线时,可以移除下面两句代码
    BaseTabBar *bTabBar = [[BaseTabBar alloc] init];
    [self setValue:bTabBar forKey:@"tabBar"];
    
    HomeViewController *home = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
    home.navigationItem.title = @"首页标题";
    home.tabBarItem.title = @"首页";
    home.tabBarItem.image = [[UIImage imageNamed:@"icon1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    home.tabBarItem.selectedImage = [[UIImage imageNamed:@"icon0"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *firstNav = [[UINavigationController alloc]initWithRootViewController:home];
    
    ShopViewController *shop = [[ShopViewController alloc] initWithNibName:@"ShopViewController" bundle:nil];
    shop.navigationItem.title = @"店铺标题";
    shop.tabBarItem.title = @"店铺";
    shop.tabBarItem.image = [self useImageName:@"icon11"];
    shop.tabBarItem.selectedImage = [[UIImage imageNamed:@"icon10"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UINavigationController *secondNav = [[UINavigationController alloc]initWithRootViewController:shop];
    
    NSMutableArray *arrays = [[NSMutableArray alloc] init];
    [arrays addObject:firstNav];
    [arrays addObject:secondNav];
    self.viewControllers = arrays;
    
    // 设置标签栏文字和图片的颜色
    self.tabBar.tintColor = [UIColor orangeColor];
    // 设置标签栏的颜色
    self.tabBar.barTintColor = [UIColor whiteColor];
    // 设置标签栏风格(默认高度49)
    self.tabBar.barStyle = UIBarStyleDefault;
    // 设置初始状态选中的下标
    self.selectedIndex = 1;
    
    // 改变UITabbar顶部分割线颜色
    CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,[[UIColor redColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setShadowImage:img];
    [self.tabBar setBackgroundImage:[[UIImage alloc] init]];
    
}

// 系统默认会把tabBar上按钮的选中图片,渲染成蓝色。这里告诉图片保持最原始的图片,不要渲染。
- (UIImage *)useImageName:(NSString *)picName {
    UIImage *image = [[UIImage imageNamed:picName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return image;
}

@end
 
UITabBar
#import "BaseTabBar.h"

@implementation BaseTabBar

- (void)layoutSubviews {
    [super layoutSubviews];
    for (UIControl *tabBarButton in self.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [self createTheDivider];
            [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}

- (void)tabBarButtonClick:(UIControl *)tabBarButton {
    for (UIView *imageView in tabBarButton.subviews) {
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            // 需要实现的帧动画
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
            animation.duration = 1;
            animation.calculationMode = kCAAnimationCubic;
            // 把动画添加上去
            [imageView.layer addAnimation:animation forKey:nil];
        }
    }
}

// 添加分割线
- (void)createTheDivider {
    NSInteger num = self.items.count;
    for (int i = 1; i < num; i++) {
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 5, 1, self.bounds.size.height - 2*5)];
        [image setBackgroundColor:[UIColor greenColor]];
        image.center = CGPointMake(([UIScreen mainScreen].bounds.size.width/num-1)*i, image.center.y);
        [self addSubview:image];
    }
}

@end


示意图:


如果您需要在 iOS 应用中弹出一个对话框并且要遮盖底部的 tabbar,可以使用 UIAlertController 来实现。UIAlertController 是一个系统自带的对话框控件,可以方便地弹出一个对话框,并且可以自定义对话框的样式和行为。 下面是一个简单的示例代码,展示如何使用 UIAlertController 弹出一个对话框并且遮盖底部的 tabbar: ``` UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { // 点击 OK 按钮后的操作 }]; [alert addAction:defaultAction]; [self presentViewController:alert animated:YES completion:nil]; ``` 在上面的代码中,我们创建了一个 UIAlertController 对象,并且设置了对话框的标题和内容。然后,我们添加了一个 UIAlertAction 对象,用于响应用户点击 OK 按钮后的操作。最后,我们调用 presentViewController 方法来展示对话框。 在展示对话框的时候,我们将 animated 参数设置为 YES,这样可以让对话框以动画的形式呈现出来。另外,我们也没有设置对话框的位置,因为系统会自动将对话框显示在屏幕中心。 需要注意的是,UIAlertController 只在 iOS 8 及以上的系统中可用,如果您的应用需要兼容 iOS 7 及以下的系统,可以考虑使用其他第三方的对话框控件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值