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;

 

 
 
 
 
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM