UI4_UIToolBar

时间:2023-03-08 22:44:44
UI4_UIToolBar
//
// AppDelegate.m
// UI4_UIToolBar
//
// Created by zhangxueming on 15/7/6.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController];
self.window.rootViewController = nav; return YES;
}
//
// ViewController.m
// UI4_UIToolBar
//
// Created by zhangxueming on 15/7/6.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h"
#import "SecondViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor cyanColor];
//每一个导航控制器都有一个导航栏
self.navigationController.toolbarHidden = NO;
// self.navigationController.toolbar.backgroundColor = [UIColor redColor];
// self.navigationController.toolbar.alpha = 0.5;
// self.navigationController.toolbar.tintColor = [UIColor redColor];
//设置背景图片
//导航栏(toolBar)的高度是44
[self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"toolBarImage@2x"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(100, 200, self.view.frame.size.width-200, 50);
[btn setTitle:@"点击" forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:24];
[btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn]; UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(btnClicked:)]; UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(btnClicked:)];
item1.tag = 200; UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"导航" style:UIBarButtonItemStylePlain target:self action:@selector(btnClicked:)];
item2.tag = 201; UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(btnClicked:)];
item3.tag = 202; NSArray *items = [NSArray arrayWithObjects:space,item1,space,item2,space,item3,space,nil];
//定制toolbarItems按钮
self.toolbarItems = items; //UIButton + UIView
//自定义ToolBar
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(0, 0, 50, 44);
btn2.frame = CGRectMake(0, 0, 50, 44);
btn3.frame = CGRectMake(0, 0, 50, 44); [btn1 setBackgroundImage:[UIImage imageNamed:@"friend_add@2x"] forState:UIControlStateNormal];
[btn2 setBackgroundImage:[UIImage imageNamed:@"friend_qq@2x"] forState:UIControlStateNormal];
[btn3 setBackgroundImage:[UIImage imageNamed:@"friend_weixin@2x"] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn3 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithCustomView:btn1];
UIBarButtonItem *item5 = [[UIBarButtonItem alloc] initWithCustomView:btn2];
UIBarButtonItem *item6 = [[UIBarButtonItem alloc] initWithCustomView:btn3]; NSArray *itemsArray = [NSArray arrayWithObjects:space,item4,space,item5,space,item6,space, nil];
self.toolbarItems = itemsArray;
} - (void)btnClicked:(UIButton *)btn
{
NSLog(@"按钮被点击");
} - (void)btnClicked
{
SecondViewController *svc = [[SecondViewController alloc] init];
[self.navigationController pushViewController:svc animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
//
// SecondViewController.m
// UI4_UIToolBar
//
// Created by zhangxueming on 15/7/6.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}