UIImage items UISegmentedControl数组不显示图像

时间:2013-11-22 13:47:01

标签: ios iphone objective-c uiimage uisegmentedcontrol

我想用Images实现SegmentedControl Bar,因此,我创建了一个带有UIImages的数组,然后我添加到了分段控件:

    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;

我将此添加到self.view的{​​{1}},但我无法在分段控件中看到图像..

这就是我看到的:enter image description here

蓝色方块而不是图片..

图片的路径没问题。

这是我的自定义实现..

ViewController

和.h

@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];
    }

}

3 个答案:

答案 0 :(得分:3)

图像必须具有正确的渲染模式。这就是我在我的子类UISegmentedControl init中所拥有的。

    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];
    }

答案 1 :(得分:0)

我尝试解决这个问题,然后打包代码。这就是代码。

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

答案 2 :(得分:0)

尝试一下:

UIImage * anImage = [[UIImage imageNamed:@"your-image-name"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self.yourSegmentedControl anImage forSegmentAtIndex:0]; // or the index that you need, in this case as example is 0