您的位置:首页 > 产品设计 > UI/UE

UIButton UIBarButtonItem用法

2013-08-02 11:29 387 查看
#pragma mark 快速创建一个item
- (UIBarButtonItem *)itemWithNormal:(NSString *)normal highlighted:(NSString *)highlighted selector:(SEL)selector {
// 初始化按钮
UIButton *button = [[[UIButton alloc] init] autorelease];
5      button.title.font = [UIFont systemFontOfSize:10];
// 设置按钮背景图片
UIImage *normalImg = [UIImage imageNamed:normal];
UIImage *highlightedImg = [UIImage imageNamed:highlighted];
button.bounds = CGRectMake(0, 0, normalImg.size.width, normalImg.size.height);
[button setBackgroundImage:normalImg forState:UIControlStateNormal];
[button setBackgroundImage:highlightedImg forState:UIControlStateHighlighted];
// 监听按钮点击
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
// 初始化item
return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
}


// 设置左边
self.navigationItem.leftBarButtonItem = [self itemWithNormal:@"left.png"
highlighted:@"leftHigh.png" selector:@selector(send)];

// 设置右边
self.navigationItem.rightBarButtonItem = [self itemWithNormal:@"right.png"
highlighted:@"rightHigh.png" selector:@selector(refresh)];


// 设置cell的背景色
UIView *bg = [[[UIView alloc] init] autorelease];
bg.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
cell.backgroundView = bg;


// 设置cell选中的背景
UIView *selectdBg = [[[UIView alloc] init] autorelease];
selectdBg.backgroundColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1];
cell.selectedBackgroundView = selectdBg;


//设置按钮的标题和颜色
[btn setNormalBg:@"normal.png" andHightlighted:@"highlight.png"];
[btn setTitle@"title" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor balckColor] forState:UIControlStateNormal];


UIButton *button = [[[UIButton alloc] init] autorelease];
//字体大小
button.title.font = [UIFont systemFontOfSize:10];
//自动对齐
button.title.textAlignment = NSTextAlignmentCenter;
//设置背景图片
[button setBackgroundImage:[UIImage resizeImage:@"image.png"] forState:UIControlStateNormal];


UIButton *btn = [[UIButton alloc] init];
UIImage *imageNormal = [UIImage imageNamed:"@image.png"];
[btn setBackgroundImage:imageNormal forState:UIControlStateNormal];
UIImage *highLight = [UIImage imageNamed:"@high.png"];
[btn setBackgroundImage:imageNormal forState:UIControlStateHighlighted];
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
viewController.navigationItem.rightBarButtonItem = item;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: