我的自定义UIView没有触发此操作,但我触发它的其他自定义视图。有谁知道这种自动刷新视图的原因是什么?或者为什么"刷新所有视图"是灰色的。
答案 0 :(得分:2)
确保您的视图具有IBInspectable
属性。 (喜欢
更改您的视图颜色,只是为了确保您的IBDesignable
事物
作品)。
确保您在视图.m
文件。 (在你的视图中绘制rect方法)。
IBDesignable
视图的文件所有者名称。(在您的storyboarD中)请务必在您的视图.h文件中提及IB_DESIGNABLE
。
最后一个qucik清理和构建你的故事板,应启用
刷新所有视图的选项。
示例代码:
for view.h
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface designalble_UIView : UIView
@property (nonatomic) IBInspectable UIColor *startColor;
@property (nonatomic) IBInspectable UIColor *midColor;
@property (nonatomic) IBInspectable UIColor *endColor;
@property (nonatomic) IBInspectable NSInteger borderWidth;
@property (nonatomic) IBInspectable CGFloat cornerRadious;
@property (nonatomic) IBInspectable BOOL isHorizontal;
@end
for view.m
#import "designalble_UIView.h"
@implementation designalble_UIView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.startColor = [UIColor redColor];
self.midColor = [UIColor greenColor];
self.endColor = [UIColor blueColor];
self.borderWidth = 2;
self.cornerRadious = 10;
self.isHorizontal = NO;
[self customInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self customInit];
}
return self;
}
- (void)drawRect:(CGRect)rect {
[self customInit];
}
- (void)setNeedsLayout {
[super setNeedsLayout];
[self setNeedsDisplay];
}
- (void)prepareForInterfaceBuilder {
[self customInit];
}
- (void)customInit {
self.layer.cornerRadius = self.cornerRadious;
self.layer.borderWidth = self.borderWidth;
if (self.cornerRadious > 0) {
self.layer.masksToBounds = YES;
}
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[self.startColor CGColor],(id)[self.midColor CGColor], (id)[self.endColor CGColor], nil];
gradient.endPoint = (self.isHorizontal) ? CGPointMake(1, 0) : CGPointMake(0, 1);
[self.layer insertSublayer:gradient atIndex:0];
}
答案 1 :(得分:1)
我解决了这个问题,因为我把IB_DESIGNABLE放在我的代码中的错误位置:
我的视图顶部有一个协议,之前看起来像是
IB_DESIGNABLE
@protocol RotaryProtocol <NSObject>
- (void) wheelDidChangeValue:(int)currentSector;
@end
@interface HCRotaryWheel : UIView
@property (weak) id <RotaryProtocol> delegate;
@property (nonatomic, strong) IBInspectable UIColor* background;
@end
要解决这个问题,我必须将IB_DESIGNABLE移到协议下面
@protocol RotaryProtocol <NSObject>
- (void) wheelDidChangeValue:(int)currentSector;
@end
IB_DESIGNABLE
@interface HCRotaryWheel : UIView
@property (weak) id <RotaryProtocol> delegate;
@property (nonatomic, strong) IBInspectable UIColor* background;
@end
现在我的观点刷新!!