2013-11-22 47 views
1

我想實現一個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; 

我添加此到ViewControllerself.view,但我看不到我的分段控制圖像..

這是我所看到的:enter image description here

藍色方塊代替圖片..

圖片的路徑沒問題。

這是我的自定義實現..

@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 
+0

也顯示.h文件 – giuseppe

+0

已編輯.. activeButtonIndex是我用來知道哪個是活動按鈕並更改所選圖像的屬性.. – croigsalvador

回答

1

的圖像必須有正確的渲染模式。這是我在我的子類UISegmentedControl初始化中所具有的。

for (int i = 0 ; i < self.imagesArray.count; i++) { 
     UIImage *image = [self.imagesArray[i] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 
     self.imagesArray[i] = image; 
     [self setImage:self.imagesArray[i] forSegmentAtIndex:i]; 
    } 
0

我試着解決這個問題,並且打包codes.here是代碼。

YYHSegmentedControl.h 


#import <UIKit/UIKit.h> 

NS_ASSUME_NONNULL_BEGIN 
typedef void (^UISegmentedControlBlock)(NSInteger index); 

@interface YYHSegmentedControl : UISegmentedControl 
@property (nonatomic, copy) UISegmentedControlBlock block; 
- (instancetype)initWithItems:(nullable NSMutableArray *)deepImageNames shallowImageNames:(NSMutableArray *)shallowImageNames; 

NS_ASSUME_NONNULL_END 
@end 


YYHSegmentedControl.m 


#import "YYHSegmentedControl.h" 
@interface YYHSegmentedControl() 

@property (nonatomic, strong) NSMutableArray *deepImageNameArr; 
@property (nonatomic, strong) NSMutableArray *shallowImageNameArr; 

@end 

@implementation YYHSegmentedControl 

- (instancetype)initWithItems:(NSMutableArray *)deepImageNames shallowImageNames:(NSMutableArray *)shallowImageNames { 
    self.deepImageNameArr = deepImageNames; 
    self.shallowImageNameArr = shallowImageNames; 
    self.tintColor = [UIColor whiteColor]; 
    self = [super initWithItems:[self dealNameItemsToImagesArr:[self getShouldDealImageNameArr:deepImageNames otherArr:shallowImageNames]]]; 
    if (self) { 
     self.selectedSegmentIndex = 0; 
     [self addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; 
    } 
    return self; 
} 

- (void)valueChange:(UISegmentedControl *)seg { 
    for (int j = 0; j < self.deepImageNameArr.count; j++) { 
     if (j == seg.selectedSegmentIndex) { 
      [self setImage:[self dealImageNameToImage:self.deepImageNameArr[seg.selectedSegmentIndex]] forSegmentAtIndex:seg.selectedSegmentIndex]; 
     }else { 
      [self setImage:[self dealImageNameToImage:self.shallowImageNameArr[j]] forSegmentAtIndex:j]; 
     } 
    } 
    self.block(seg.selectedSegmentIndex); 
} 

- (NSMutableArray *)getShouldDealImageNameArr:(NSMutableArray *)firstArr otherArr:(NSMutableArray *)secondArr { 
    NSMutableArray *arr = [NSMutableArray array]; 
    [arr addObject:firstArr[0]]; 
    for (int i = 1; i<secondArr.count; i++) { 
     [arr addObject:secondArr[i]]; 
    } 
    return arr; 
} 

- (NSMutableArray *)dealNameItemsToImagesArr:(NSMutableArray *)imageNames { 
    NSMutableArray *arr = [NSMutableArray array]; 
    for (int i = 0; i < imageNames.count; i++) { 
     UIImage *image = [[UIImage imageNamed:imageNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 
     [arr addObject:image]; 
    } 
    return arr; 
} 

- (UIImage *)dealImageNameToImage:(NSString *)imageName { 
    UIImage *image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 
    return image; 
} 

@end