2010-11-22 6 views
1

뷰 컨트롤러와 함께 내 뷰의 레이아웃을 제어하기 위해 UIView를 사용하고 있습니다. UIScrollView가 수직 화면의 절반 만 사용하기를 원합니다. 스크린의 상반부를 사용하면 좋지만 하반부는 사용하지 않아도됩니다.UIScrollView를 포함하는 UIView의 레이아웃을 존중하게 만드는 방법은 무엇입니까?

여기에있는 UIViewController에서 관련 코드는 다음과 같습니다

- (void)loadView { 
CGRect fullFrame = [[UIScreen mainScreen] applicationFrame]; 
//trying to put the scroll view on the bottom half of the screen, but does not work. 
CGRect halfFrame = CGRectMake(0, fullFrame.size.height/2 , 
    fullFrame.size.width, fullFrame.size.height/2); 
//use this instead for the scroll view to go to the top half of the screen (and work properly) 
//CGRect halfFrame = CGRectMake(0, 0 , fullFrame.size.width, fullFrame.size.height/2); 

UIScrollView* sv = [[UIScrollView alloc] initWithFrame:halfFrame]; 
[sv setContentSize:CGSizeMake(3 * halfFrame.size.width, halfFrame.size.height)]; 

CGRect stencilFrame = halfFrame; 
UIView *leftView = [[UIView alloc] initWithFrame:stencilFrame]; 

stencilFrame.origin.x += stencilFrame.size.width; 
UIView *centerView = [[UIView alloc] initWithFrame:stencilFrame]; 

stencilFrame.origin.x += stencilFrame.size.width; 
UIView *rightView = [[UIView alloc] initWithFrame:stencilFrame]; 

//mix up the colors 
[leftView setBackgroundColor:[UIColor redColor]]; 
[centerView setBackgroundColor:[UIColor greenColor]]; 
[rightView setBackgroundColor:[UIColor blueColor]]; 

//add them to the scroll view 
[sv addSubview:leftView]; 
[sv addSubview:centerView]; 
[sv addSubview:rightView]; 

//turn on paging 
[sv setPagingEnabled:YES]; 

UIView *containerView = [[UIView alloc]initWithFrame:fullFrame]; 
[containerView addSubview:sv]; 
[self setView:containerView];  
} 

어떤 조언이나 도움에 미리 감사드립니다.

답변

1

알아 냈습니다. 문제의 핵심은 스크롤 뷰 내의 뷰가 스크롤 뷰 자체와 동일한 프레임으로 초기화된다는 것입니다. scrollView가 halfFrame으로 초기화되면 원점은 (전체 화면 크기의 절반 인) 0입니다. 이는 응용 프로그램 창 자체와 관련이 있으므로 ok입니다. 그러나 scrollView 안에 들어있는 뷰 (예 : leftView)는 halfFrame으로 초기화되지만,이 경우 원점은 scrollView를 기준으로하므로 효과적으로 화면에서 벗어납니다. 원점을 (0,0)으로 설정하면 다음을 수정합니다.

CGRect 스텐실 프레임 = CGRectMake (0, 0, fullFrame.size.width, fullFrame.size.width)

0

contentSize에는 스크롤보기 내부에보기의 사각형이 있어야합니다. 즉, 스크롤 가능한 모든 컨트롤의 전체 크기입니다. UIScrollView의 프레임은 사용자가 모든 것을 탐색 할 수 있도록 스크롤이 얼마나 필요한지 결정합니다.

+0

나는 확실히 따를 수 없다. "스크롤 가능한 컨트롤의 전체 크기"가 개별 뷰의 3 * 프레임 너비와 같습니다 (뷰가 3 개이므로)? –

0

탐색 표시 줄이나 탭 표시 줄이있는 경우 "전체 프레임"을 사용할 수 없습니다. 일반적으로 레이아웃 정보로 [UIScreen mainScreen]을 사용하는 코드는 잘못된 것입니다.

또한 (예를 들어) 통화가 진행 중이거나 테 더링이 활성화 된 경우 상태 표시 줄의 크기가 변경 될 수 있습니다.

대신, 전체 프레임에 대해 어떤 제정신 값을 사용하고 자동 크기 수 : 할 -layoutSubviews을 당신도 아마 그것은 또한 콘텐츠 크기를 설정하는 것이있는 UIScrollView의 하위 클래스 등 -setFrame:를 구현해야과 :

CGRect fullFrame = {{0,0}, {320,480}}; 

... 

sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin; 

편집을 올바른 레이아웃.

+0

프레임 크기에 MainScreen을 사용하는 것에 대한 귀하의 요지를 이해합니다. 그러나 자동 크기 조정 마스크를 사용하는 경우에도 프레임 크기 (찾은 프레임 크기)에서는 작동하지 않습니다. –

+0

그래서 필요에 따라 서브 클래스를 만들고 -layoutSubviews를 구현하십시오. –

+0

입력 해 주셔서 감사합니다. 아래에 게시 한 간단한 솔루션을 발견했습니다. –

관련 문제