2011-11-26 2 views
0

사용 가능한 모든 UIToolbar 공간을 채우기 위해 UISlider의 크기를 조정해야하며 iOS 4.3 및 iOS 5.0에서 작동해야합니다. (글쎄, 나는 5.0에서 작동하지만, 4.3에서는 여전히 퍼즐이다.)사용 가능한 모든 UIToolbar 공간 (iOS)을 채우려면 컨트롤 (UISlider)의 크기를 조절해야합니다.

가로 방향으로 만 나타나는 버튼을 포함하여 버튼이 동적으로 구성된 UIToolbar가있다. (전체 세트는 인터페이스 빌더에서 지정되며 그 중 일부는 bar.items = filteredItems을 통해 제거됩니다.) 왼쪽과 오른쪽의 단추 사이에는 슬라이더 (그리고 그 옆에 유연한 공간)가 있습니다.

두 개의 단추 그룹 (왼쪽 그룹과 오른쪽 그룹) 사이의 사용 가능한 모든 공간을 슬라이더에 채우고 싶습니다.

답변

1

이렇게하면 모든 막대 버튼 항목의 너비를 계산합니다.

주요 아이디어는 그들의 순서는 중요하지 않은 것입니다 그리고 우리는 IBOutletCollection 사용할 수 있습니다

@property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *allButtons; 

및 방향 변경 전에 실행됩니다 코드에서 :

const CGFloat leftOrRightMargin = 12; // measured on a screenshot 
const CGFloat gapBetweenButtons = 10; // measured on a screenshot 
const CGFloat experimentalCorrection = 6; // don't ask why 

int totalNumberOfButtons = 0; 
CGFloat totalWidthOfButtons = 0.0; 
for (UIButton *b in allButtons) { 
    if (b.superview) { // if it's visible 
     totalNumberOfButtons++; 
     totalWidthOfButtons += b.bounds.size.width; 
    } 
} 
CGFloat occupiedSpace = experimentalCorrection 
         + 2 * leftOrRightMargin 
         // gaps: totalNumberOfButtons-1+1, the last one is for the slider 
         + totalNumberOfButtons * gapBetweenButtons 
         + totalWidthOfButtons; 
// now, resize the slider control 

방향 변경 후 :

[myBar setNeedsLayout]; 
+0

UISearchBar를 사용하여이 작업을 수행하려고합니다 ... 나는 더러워진 솔을 찾지 못했을 것입니다. ution? –

+0

IB에서 내 검색 창을 추가하면 방향 변경으로 크기가 조정 된 것 같습니다. 그런 다음 항목 수가 적은 툴바에서 항목을 다시 설정하면 검색 막대의 크기는 조정되지 않지만 이전 크기로 유지됩니다. 절망적이다. –

관련 문제