我如何获得自动刷新视图的工作?
确保你有你的观点
IBInspectable
性能。 (像 改变你的视图颜色,只是为了确保你的IBDesignable
东西 工作)。确保您有您的自定义代码在你看来.M
文件。 (在你的视图中绘制矩形方法)。
- 确保您的
IBDesignable
视图提供文件的所有者名称。(在你的故事板) 确保你对你的看法.h文件中提到
IB_DESIGNABLE
。最后一个qucik清理并构建您的故事板,将启用刷新所有视图的
选项。
示例代码:
为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
为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];
}
非常感谢您的回复 - 遗憾的是我的代码包含了所有这些内容。我可以让这些按钮不会变灰的唯一方法是如果我放入另一个自定义视图,将其删除,然后将其放回并单击“刷新所有视图”,但此自定义视图从不触发“自动刷新视图”。由于我有大量的代码,我不能在这里发布它,但它在那里:https://github.com/HannahCarney/RotaryWheel –
我会研究它! :) –
非常感谢你! –
我解决d这一点,因为我已经在我的代码放在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
现在我的意见刷新!
您使用IBDesignable/IBInspectable吗? –
是的,我在这个特殊情况下不会刷新视图 –