【发布时间】:2017-03-04 21:30:59
【问题描述】:
在 SO 上有几个问题问这个问题,但没有一个问题问得正确。
我正在使用一个自定义进度条,它是一个基于 UIView 的类和这个实现。
@implementation MCProgressBarView {
UIImageView * _backgroundImageView;
UIImageView * _foregroundImageView;
CGFloat minimumForegroundWidth;
CGFloat availableWidth;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
// [self bounds] is 1000x1000 here... what?
if (self) [self initialize];
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) [self initialize];
return self;
}
- (void) initialize {
UIImage * backgroundImage = [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"progress-bg" ofType:@"png"]]
resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, 10.0f, 0.0f, 10.0f)];
UIImage * foregroundImage = [[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"progress-bg" ofType:@"png"]]
resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, 10.0f, 0.0f, 10.0f)];
_backgroundImageView = [[UIImageView alloc] initWithFrame:self.bounds];
_backgroundImageView.image = backgroundImage;
[self addSubview:_backgroundImageView];
_foregroundImageView = [[UIImageView alloc] initWithFrame:self.bounds];
_foregroundImageView.image = foregroundImage;
[self addSubview:_foregroundImageView];
UIEdgeInsets insets = foregroundImage.capInsets;
minimumForegroundWidth = insets.left + insets.right;
availableWidth = self.bounds.size.width - minimumForegroundWidth;
[self adjustProgress:0.0f];
}
我在界面生成器上创建了一个 UIView 为 200x20 点,并将该视图分配给我正在使用的这个自定义类 MCProgressBarView。当应用程序运行时,initWithCoder 运行并创建一个大小为 1000x1000 点的进度视图,而不管视图在 IB 上的大小。
如何强制 initWithCoder 以分配给 IB 的正确大小运行?
注意:我不想将它硬连接到 200x20,也不想在运行时通过代码设置它。有没有办法让这个工作?
【问题讨论】:
标签: ios iphone ipad uiview initwithcoder