UIView
의 서브 클래스를 만들 때 대개 내 모든 초기화 및 레이아웃 코드를 init
메서드에 배치합니다. 하지만 레이아웃 코드는 layoutSuviews
을 재정 의하여 수행해야한다고 들었습니다. SO가 post인데, 각각이라는 메서드가 호출 될 때 을 설명하지만 실제로 사용하는 방법을 알고 싶습니다.UIView : layoutSubviews 대 initWithFrame
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;
}
그것은 일반적인 작업입니다 : 설정, 뷰의 프레임을 설정 바르를 초기화하는 UIImages 초기화, UIImageViews에 배치, 그들을 밖으로 배치 할 수 있습니다.
제 질문은 : init
중 어느 것을 수행해야합니까? layoutSubviews
?
나는 그것을 더 아래로 밀 것이다. init이 아닌'viewDidLoad'에서 서브 뷰 객체를 생성하십시오. 뷰가 지연되어로드되므로 수퍼 뷰를 만들지 않을 경우 하위 뷰를 만들 필요가 없습니다. – Abizern
우리는 여기서 ViewController가 아니라 View에 대해 이야기하고 있습니다. – calimarkus