UIButton UIBarButtonItem用法

 1 #pragma mark 快速创建一个item
 2 - (UIBarButtonItem *)itemWithNormal:(NSString *)normal highlighted:(NSString *)highlighted selector:(SEL)selector {
 3     // 初始化按钮
 4     UIButton *button = [[[UIButton alloc] init] autorelease];
 5      button.title.font = [UIFont systemFontOfSize:10];
6 // 设置按钮背景图片 7 UIImage *normalImg = [UIImage imageNamed:normal]; 8 UIImage *highlightedImg = [UIImage imageNamed:highlighted]; 9 button.bounds = CGRectMake(0, 0, normalImg.size.width, normalImg.size.height); 10 [button setBackgroundImage:normalImg forState:UIControlStateNormal]; 11 [button setBackgroundImage:highlightedImg forState:UIControlStateHighlighted]; 12 // 监听按钮点击 13 [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside]; 14 // 初始化item 15 return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease]; 16 }
1 // 设置左边
2 self.navigationItem.leftBarButtonItem = [self itemWithNormal:@"left.png" 
highlighted:@"leftHigh.png" selector:@selector(send)]; 3 4 // 设置右边 5 self.navigationItem.rightBarButtonItem = [self itemWithNormal:@"right.png"
highlighted:@"rightHigh.png" selector:@selector(refresh)];
1 // 设置cell的背景色
2  UIView *bg = [[[UIView alloc] init] autorelease];
3  bg.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
4   cell.backgroundView = bg;
1 // 设置cell选中的背景
2 UIView *selectdBg = [[[UIView alloc] init] autorelease];
3 selectdBg.backgroundColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1];
4 cell.selectedBackgroundView = selectdBg;
1 //设置按钮的标题和颜色
2 [btn setNormalBg:@"normal.png" andHightlighted:@"highlight.png"];
3 [btn setTitle@"title" forState:UIControlStateNormal];
4 [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];
1 UIButton *btn = [[UIButton alloc] init];
2 UIImage *imageNormal = [UIImage imageNamed:"@image.png"];
3 [btn setBackgroundImage:imageNormal forState:UIControlStateNormal];
4 UIImage *highLight = [UIImage imageNamed:"@high.png"];
5 [btn setBackgroundImage:imageNormal forState:UIControlStateHighlighted];
6 UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
7 viewController.navigationItem.rightBarButtonItem = item;
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/wangshengl9263/p/3232200.html