iOS之UIToolBar的使用

一、UIToolBar的基本用法

1、.h

#import <UIKit/UIKit.h>

@interface FKViewController : UIViewController
- (IBAction)clicked:(id)sender;
@end

2、.m

#import "FKViewController.h"

@interface FKViewController ()

@end

@implementation FKViewController

- (void)viewDidLoad
{
	[super viewDidLoad];
}
- (IBAction)clicked:(id)sender {
	// 使用UIAlertView显示用户点击了哪个按钮
	NSString* msg = [NSString stringWithFormat:@"您点击了【%@】按钮"
		, [sender title]];
	UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
		message:msg
		delegate:nil
		cancelButtonTitle:@"确定"
		otherButtonTitles: nil];
	[alert show];
}
@end

二、自定义的UIToolBar

纯代码实现

#import "FKViewController.h"

@interface FKViewController ()

@end

@implementation FKViewController
UIProgressView* prog;
NSTimer* timer;
- (void)viewDidLoad
{
    [super viewDidLoad];
	// 创建一个工具条,并设置它的大小和位置
	UIToolbar *myToolbar = [[UIToolbar alloc]
		initWithFrame:CGRectMake(0, 20, 320, 44)];
	// 将工具条添加到当前应用的界面中
	[self.view addSubview:myToolbar];
	// 创建使用文本标题的UIBarButtonItem
	UIBarButtonItem* bn1 = [[UIBarButtonItem alloc]
		initWithTitle:@"OK"
		style:UIBarButtonItemStylePlain
		target:self
		action:@selector(clicked:)];
	// 创建使用自定义图片的UIBarButtonItem	
	UIBarButtonItem* bn2 = [[UIBarButtonItem alloc]
		initWithImage:[UIImage imageNamed:@"heart.gif"]
		style:UIBarButtonItemStyleBordered
		target:self
		action:@selector(clicked:)];
	// 创建使用系统图标的UIBarButtonItem
	UIBarButtonItem* bn3 = [[UIBarButtonItem alloc]
		initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
		target:self
		action:@selector(clicked:)];
	// 创建一个可伸缩的UIBarButtonItem
	UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
		initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
		target:nil
		action:nil];
	prog = [[UIProgressView alloc]
		initWithProgressViewStyle:UIProgressViewStyleBar];
	// 设置UIProgressView的大小
	prog.frame = CGRectMake(0 , 0 , 80, 20);
	// 设置该进度条的初始进度为0
	prog.progress = 0;
	// 创建使用UIView的UIBarButtonItem
	UIBarButtonItem *bn4 = [[UIBarButtonItem alloc]
		initWithCustomView:prog];
	// 为工具条设置工具按钮
	myToolbar.items = [NSArray arrayWithObjects:
		bn1	, bn2, bn3,flexItem, bn4, nil];
	timer = [NSTimer scheduledTimerWithTimeInterval:0.2
		target:self selector:@selector(changeProgress)
		userInfo:nil repeats:YES];
}
- (void) clicked:(id)sender
{
	NSLog(@"%@", sender);
}
- (void) changeProgress
{
	// 如果进度满了,停止计时器
	if(prog.progress >= 1.0)
	{
		// 停用计时器
		[timer invalidate];
	}
	else
	{
		// 改变进度条的进度值
		[prog setProgress:prog.progress + 0.02 animated:YES];
	}
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值