我想實現一個SegmentedControl酒吧與圖片,所以,我已創建了UIImages數組,然後我加入分段控制:UIImage的項目UISegmentedControl的陣列不顯示圖像
UIImage *img1 = [UIImage imageNamed:@"btn-checkin.png"];
UIImage *img2 = [UIImage imageNamed:@"btn-checkin.png"];
UIImage *img3 = [UIImage imageNamed:@"btn-fav-selected.png"];
UIImage *img4 = [UIImage imageNamed:@"btn-fav.png"];
NSArray *itemsArray = [NSArray arrayWithObjects:img1,img2,img3,img4,nil];
segmentedControlBar = [[WPCustomSegmentedBar alloc] initWithItems:itemsArray];
segmentedControlBar.frame = toolBarFrame;
我添加此到ViewController
的self.view
,但我看不到我的分段控制圖像..
這是我所看到的:
藍色方塊代替圖片..
圖片的路徑沒問題。
這是我的自定義實現..
@implementation WPCustomSegmentedBar
- (id)initWithItems:(NSArray *)items
{
self = [super initWithItems:items];
if (self) {
self.segmentedControlStyle = UISegmentedControlStyleBar;
self.selectedSegmentIndex = UISegmentedControlNoSegment;
[self addTarget:self action:@selector(valueChanged:) forControlEvents: UIControlEventValueChanged];
}
return self;
}
- (void)valueChanged:(UISegmentedControl *)segment {
[self disableButtonImage];
if(segment.selectedSegmentIndex == 0) {
[self setImage:[UIImage imageNamed:@"btn-checkin-selected.png"] forSegmentAtIndex:segment.selectedSegmentIndex];
}else if(segment.selectedSegmentIndex == 1){
[self setImage:[UIImage imageNamed:@"btn-info-selected.png"] forSegmentAtIndex:segment.selectedSegmentIndex];
}else if(segment.selectedSegmentIndex == 2){
[self setImage:[UIImage imageNamed:@"btn-comment-selected.png"] forSegmentAtIndex:segment.selectedSegmentIndex];
}else if(segment.selectedSegmentIndex == 3){
[self setImage:[UIImage imageNamed:@"btn-fav-selected.png"] forSegmentAtIndex:segment.selectedSegmentIndex];
}
self.activeButtonIndex = segment.selectedSegmentIndex;
}
-(void) disableButtonImage
{
if(activeButtonIndex == 0) {
NSLog(@"active button 0");
[self setImage:[UIImage imageNamed:@"btn-checkin.png"] forSegmentAtIndex:activeButtonIndex];
}else if(activeButtonIndex == 1){
NSLog(@"active button 1");
[self setImage:[UIImage imageNamed:@"btn-info.png"] forSegmentAtIndex:activeButtonIndex];
}else if(activeButtonIndex== 2){
NSLog(@"active button 2");
[self setImage:[UIImage imageNamed:@"btn-comment.png"] forSegmentAtIndex:activeButtonIndex];
}else if(activeButtonIndex == 3){
NSLog(@"active button 3");
[self setImage:[UIImage imageNamed:@"btn-fav.png"] forSegmentAtIndex:activeButtonIndex];
}
}
和.h
#import <UIKit/UIKit.h>
@interface WPCustomSegmentedBar : UISegmentedControl
@property (nonatomic)NSInteger activeButtonIndex;
@end
也顯示.h文件 – giuseppe
已編輯.. activeButtonIndex是我用來知道哪個是活動按鈕並更改所選圖像的屬性.. – croigsalvador