サブクラス化するときUIView
、私は通常、すべての初期化コードとレイアウトコードをそのinit
メソッドに配置します。しかし、レイアウトコードはオーバーライドして実行する必要があると言われていますlayoutSuviews
。各メソッドがいつ呼び出されるかを説明するSOに関する投稿がありますが、実際にそれらを使用する方法を知りたいです。
私は現在、次のinit
ようにすべてのコードをメソッドに配置しています。
MyLongView.m
- (id)initWithHorizontalPlates:(int)theNumberOfPlates
{
self = [super initWithFrame:CGRectMake(0, 0, 768, 1024)];
if (self) {
// Initialization code
_numberOfPlates = theNumberOfPlates;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
[scrollView setContentSize:CGSizeMake(self.bounds.size.width* _numberOfPlates, self.bounds.size.height)];
[self addSubview:scrollView];
for(int i = 0; i < _numberOfPlates; i++){
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"a1greatnorth_normal_%d.jpg", i+1]];
UIImageView *plateImage = [[UIImageView alloc] initWithImage:img];
[scrollView addSubview:plateImage];
plateImage.center = CGPointMake((plateImage.bounds.size.width/2) + plateImage.bounds.size.width*i, plateImage.bounds.size.height/2);
}
}
return self;
}
これは通常のタスクです。ビューのフレームの設定、ivarの初期化、スクロールビューの設定、UIImageの初期化、UIImageViewsへの配置、レイアウトです。
私の質問は、これらのどれをで行うべきかinit
、そしてこれらのどれをで行うべきlayoutSubviews
かということです。