SOにこれを求めるいくつかの質問がありますが、誰もそれを正しく尋ねていません。initWithCoderで1000x1000を表示してください:
私はこの実装で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];
}
私は200x20のポイントであることをインターフェイスビルダーでUIView
を作成し、私が使用しています。このカスタムクラスMCProgressBarView
のものであると、そのビューを割り当てています。アプリケーションが実行されると、initWithCoder
が実行され、1000x1000ポイントのサイズの進捗ビューが作成されます。ビューのサイズはIBでは無視されます。
IBに割り当てられた適切なサイズでinitWithCoderを動作させるにはどうすればよいですか?
注:200x20にハードワイヤ接続したくないので、実行時にこのコードを設定したくありません。この仕事をする方法はありますか?
UIViewベースのクラスではviewDidLayoutSubviewsはありません。 viewControllerを使用してビューのサイズを正しく設定すると、このことはすべて、AppleがiOS上で多くのことを思い描いていません。 – SpaceDog
@SpaceDog 'layoutSubviews'を使用してください。 –
私はクラスに 'layoutSubviews'を実装しました。そのメソッドが実行されるとき、私は何もしなくてもビューは正しいサイズで表示されますが、アプリケーションに表示されるときは1000x1000です。 – SpaceDog