【问题标题】:IB_DESIGNABLE tintColor issuesIB_DESIGNABLE tintColor 问题
【发布时间】:2015-05-31 12:47:18
【问题描述】:

我正在进入 IB_DESIGNABLE,但遇到了一个问题。

当我使用 IB 设置自定义视图的 tintColor 时,它会在 IB 中以正确的方式呈现。

但是当我在设备上运行它时,它会以默认的 tintColor 显示。

#pragma mark - UIView

- (void)drawRect:(CGRect)rect {
    [self drawCircleRadius:MIN(rect.size.width / 2, rect.size.height / 2) - self.lineWidth / 2.f
                      rect:rect
                startAngle:self.startAngleRadians
                  endAngle:self.endAngleRadians
                 lineWidth:self.lineWidth];
}

#pragma mark - private methods

- (void)drawCircleRadius:(CGFloat)radius
                    rect:(CGRect)rect
              startAngle:(CGFloat)startAngle
                endAngle:(CGFloat)endAngel
               lineWidth:(CGFloat)lineWidth {
    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
    [self.tintColor setStroke];
    [bezierPath addArcWithCenter:CGPointMake(rect.size.width / 2, rect.size.height / 2)
                          radius:radius
                      startAngle:startAngle
                        endAngle:endAngel
                       clockwise:YES];

    bezierPath.lineWidth = lineWidth;
    [bezierPath stroke];
}

有什么区别?为什么在设备中以默认的tint color显示,而在IB中却正确显示?

更新:

#import <UIKit/UIKit.h>

IB_DESIGNABLE
@interface PKCircleView : UIView

@property (nonatomic, assign) IBInspectable CGFloat startAngleRadians;
@property (nonatomic, assign) IBInspectable CGFloat endAngleRadians;
@property (nonatomic, assign) IBInspectable CGFloat lineWidth;

@end

【问题讨论】:

  • 我运行了您的代码,它在 IB、模拟器和设备上运行良好。问题一定出在其他地方。也许显示自定义视图的头文件?这是我使用的:#import &lt;UIKit/UIKit.h&gt; IB_DESIGNABLE @interface Circle : UIView @property (nonatomic) IBInspectable double lineWidth; @property (nonatomic) IBInspectable double startAngleRadians; @property (nonatomic) IBInspectable double endAngleRadians; @end
  • @picciano 是的,看起来一样。

标签: ios interface-builder uicolor tintcolor ibdesignable


【解决方案1】:

问题就在那一行

 self.tintColor = [UIColor defaultDwonloadButtonBlueColor];

static PKCircleView *CommonInit(PKCircleView *self) {
    if (self != nil) {
        self.backgroundColor = [UIColor clearColor];
        self.startAngleRadians = M_PI * 1.5;
        self.endAngleRadians = self.startAngleRadians + (M_PI * 2);
        self.lineWidth = 1.f;
        self.tintColor = [UIColor defaultDwonloadButtonBlueColor];
    }
    return self;
}

@implementation PKCircleView

#pragma mark - initialization

- (id)initWithCoder:(NSCoder *)decoder {
    return CommonInit([super initWithCoder:decoder]);
}

- (instancetype)initWithFrame:(CGRect)frame {
    return CommonInit([super initWithFrame:frame]);
}

似乎在 init... 方法之前调用了来自 IB 的 setTintColor。

【讨论】:

    猜你喜欢
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多