UINavigationController, UITabBarController

UITabBarController

通用

AppDelegate.m

    self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];

    ViewController *vc = [[ViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:vc];
    // 创建 TabBar 按钮的样式
    naVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1000];

    SecondViewController *secVC = [[SecondViewController alloc] init];
    UINavigationController *secNAVC = [[UINavigationController alloc] initWithRootViewController:secVC];
    secNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"shop" image:[UIImage imageNamed:@"1.png"] selectedImage:[UIImage imageNamed:@"2.png"]];

    // 创建 tabBarController
    UITabBarController *tab = [[UITabBarController alloc] init];
    tab.viewControllers = @[naVC, secNAVC, ...];
    // 给 window 设置根视图控制器
    self.window.rootViewController = tab;

    // 编程的时候, 设置默认的出现页面,不用每次都从第0个开始
    tab.selectedIndex = 3;

    tab.delegate = self;

    [tab release];
    [naVC release];
    [vc release];

property

naVC.tabBarItem.badgeValue 小红点的内容, 无小红点时为 nil
naVC.tabBarItem 创建 tabBar 按钮的样式
eg.

naVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1000];
或者
naVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"shop" image:[UIImage imageNamed:@"1.png"] selectedImage:[UIImage imageNamed:@"2.png"]];

UITabBarController的外观设置
tabBar.translucent 透明
tabBar.barTintColor 背景颜色
tabBar.tintColor 图标被选中时颜色
selectedIndex 默认出现的页面编号

protocol

-(void) tabBarController: (UITabBarController * )tabBarController didSelectViewController:(UIViewController * )viewController

UINavigationController

通用

AppDelegate.m

#import "ViewController.h"

    self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    [_window release];
    ViewController *vc = [[ViewController alloc] init];
    // 创建导航视图控制器
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:vc];
    // 把导航视图控制器设置成window的视图控制器
    self.window.rootViewController = naVC;
    [naVC release];
    [vc release];
    return YES;
}

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

设置外观

    self.navigationItem.title = @"导航栏标题";

property

  • navigationController.navigationBar.translucent 影响坐标系的起点:默认 半透明YES(0, 0), 不透明NO(0, 64)
  • navigationController.navigationBar.barTintColor 背景颜色
  • navigationItem.title 标题
  • navigationItem.titleView 标题可以用视图代替
  • navigationItem.leftBarButtonItem(s) 创建左右两边的按钮(们)
  • navigationItem.rightBarButtonItem
    // 创建系统自带的按钮
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(barAction:)] autorelease];

// 模态跳转 view往secondView跳转
- (void) buttonAction:(UIButton *) button {
    SecondViewController *loginVC = [[SecondViewController alloc] init];
    [secVC setModalTransitionStyle: UIModalTransitionStyleCrossDissolve];
    [self presentViewController: secVC animated:YES completion:^{

    }];
}
    // 按钮用自定义的图片,图片颜色固定是蓝色
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"1.png"] style:UIBarButtonItemStylePlain target:self action:@selector(commentAction:)];
    // 用自定义视图创建按钮,让图片编程原来的颜色
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [myButton setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    myButton.frame = CGRectMake(0, 0, 40, 40);
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView: button] autorelease];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值