我知道可以使用 UIImageView
来设置 Disclosure Indicator Accessory,但我想在不使用 UIImageView
的情况下仅更改 disclosure indicator 颜色。
这是否可能?如果可能的话,该怎么做呢?
我知道可以使用 UIImageView
来设置 Disclosure Indicator Accessory,但我想在不使用 UIImageView
的情况下仅更改 disclosure indicator 颜色。
这是否可能?如果可能的话,该怎么做呢?
添加您自己的披露指示器:
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory.png"]];
我知道我来晚了,但我刚刚准备了一个自定义视图,绘制了一个披露视图。我为演示快速制作了它,所以可能不是非常精确,但它能完成工作。希望能对某些人有所帮助:
PZDisclosureIndicator.h
//
// Created by Pall Zoltan on 10/10/14.
// Copyright (c) 2014 pallzoltan. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface PZDisclosureIndicator : UIView
@property(nonatomic, strong) UIColor *color;
- (PZDisclosureIndicator *)initWithColor:(UIColor *)color;
@end
PZDisclosureIndicator.m:
//
// Created by Pall Zoltan on 10/10/14.
// Copyright (c) 2014 pallzoltan. All rights reserved.
//
#import "PZDisclosureIndicator.h"
@interface PZDisclosureIndicator ()
@property(nonatomic) CGFloat red;
@property(nonatomic) CGFloat green;
@property(nonatomic) CGFloat blue;
@property(nonatomic) CGFloat alpha;
@end
@implementation PZDisclosureIndicator
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, self.red, self.green, self.blue, self.alpha);
CGContextMoveToPoint(context, 4, 0);
CGContextAddLineToPoint(context, 4, 0);
CGContextAddLineToPoint(context, 16, 12);
CGContextAddLineToPoint(context, 4, 24);
CGContextAddLineToPoint(context, 0, 24 - 4);
CGContextAddLineToPoint(context, 9, 12);
CGContextAddLineToPoint(context, 0, 4);
CGContextAddLineToPoint(context, 4, 0);
CGContextFillPath(context);
}
- (PZDisclosureIndicator *)initWithColor:(UIColor *)color {
self = [super initWithFrame:CGRectMake(0, 0, 16, 24)];
self.backgroundColor = [UIColor clearColor];
self.color = color;
[self setNeedsDisplay];
return self;
}
- (void)setColor:(UIColor *)color {
_color = color;
[self.color getRed:&_red green:&_green blue:&_blue alpha:&_alpha];
[self setNeedsDisplay];
}
@end
self.accessoryView = [[PZDisclosureIndicator alloc] initWithColor:SECONDARY_HIGHLIGHT_COLOR];
CGContextSetLineWidth(ctxt, 2);
将其改为2而不是4,以获得所需的结果。 - Ryan Brodieclass GratuitousDisclosureTableViewCell: UITableViewCell {
private weak var disclosureButton: UIButton? {
didSet {
if let originalImage = self.disclosureButton?.backgroundImageForState(.Normal) {
let templateImage = originalImage.imageWithRenderingMode(.AlwaysTemplate)
self.disclosureButton?.setBackgroundImage(templateImage, forState: .Normal)
self.disclosureButton?.setBackgroundImage(templateImage, forState: .Highlighted)
self.disclosureButton?.setBackgroundImage(templateImage, forState: .Disabled)
self.disclosureButton?.setBackgroundImage(templateImage, forState: .Selected)
self.disclosureButton?.setBackgroundImage(templateImage, forState: .Application)
self.disclosureButton?.setBackgroundImage(templateImage, forState: .Reserved)
}
}
}
override func awakeFromNib() {
super.awakeFromNib()
for view in self.subviews {
if let button = view as? UIButton {
self.disclosureButton = button
break
}
}
}
}
awakeFromNib
中添加 self.tintColor = .red
,然后再遍历 subViews
,我认为这个子类会更清晰一些。 - Daniel Storm您需要创建自己的自定义UIButton并将其设置为单元格的accessoryView。您不能仅通过指定其UIColor来更改其颜色。
您可以通过添加一张用于正常状态和另一张用于选中(高亮)状态的图像来自定义披露指示器。
一个用于自定义披露指示器的库:在这里查看。
let font = UIFont(name: "icomoon", size: 16.0)
let icon = "\u{e60f}"
let lbl = UILabel(frame: CGRectMake(0, 0, 20, 20))
lbl.font = font
lbl.text = icon
cell.accessoryView = lbl
UIFont(name: "icomoon"
。至少有几个人会认识到这不是 iOS 默认嵌入的字体。 - njzk2cell.accessoryView = myAccessoryUIImageView;
cell accessoryview uiimageview not align correctly try below code:
UIView* accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 50)];
UIImageView* accessoryViewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory_image.png"]];
accessoryViewImage.center = CGPointMake(12, 25);
[accessoryView addSubview:accessoryViewImage];
[cell setAccessoryView:accessoryView];