2011-08-11 3 views
0

UIViewController에서 프로그램 아래쪽 도구 모음에 2 UIBarButtonItem을 추가하려고합니다. 지금 버튼을 만들고, NSArray에 추가 한 다음 [self setToolbarItems:buttonArray animated:YES]을 호출합니다. 이렇게하면 도구 모음의 왼쪽 하단 모서리에 단추를 추가 할 수 있습니다. 내가하고 싶은 일은 도구 모음의 왼쪽 하단에 하나의 단추를 추가하고 오른쪽 아래에 다른 단추를 추가하는 것입니다. 이 일을 어떻게 하죠?setToolbarItems를 사용할 때 레이아웃을 어떻게 제어합니까?

답변

4

귀하의 toolBarItems 그렇게 보일 것이다 : 그것은 아이폰 OS처럼 보이는

enter image description here

0

단추 사이에 UIBarButtonSystemItemFlexibleSpace이라는 시스템 표시 줄 단추 항목을 추가 할 수 있습니다. 그것들을 옆으로 밀 것이다.

+0

나는 첫 번째 항목과 마지막 항목에 대한 항목을 가지고 있으며 iOS가 순환계에서 도구 모음 항목 레이아웃을 새로 고치지 않습니다. – Carl

2

당신은 바 버튼 시스템 항목 UIBarButtonSystemItemFlexibleSpace

UIBarButtonItem *leftButton; // set that 
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
UIBarButtonItem *rightButton; // set that 

NSArray *items = [NSArray arrayWithObjects:leftButton, flexibleSpace, rightButton, nil]; 
[toolbar setItems:items]; 

[flexibleSpace release]; 
// release the other two if necessary 

당신이 수동에 폭 속성을 설정하여 간격을 제어하려는 경우 당신은 또한 바 버튼 시스템 항목 UIBarButtonSystemItemFixedSpace을 사용할 수를 사용해야합니다.

+0

처음 항목과 마지막 항목에 대한 항목이 있고 iOS가 순환 게재에서 도구 모음 항목 레이아웃을 새로 고치지 않습니다. – Carl

0

이에 도구 모음의 레이아웃을 새로 고침하지 않습니다

UIBarButtonItem *firstButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(someSelector)]; 
UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(someSelector)]; 

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

self.toolbarItems = [NSArray arrayWithObjects:firstButton, flexibleSpace, secondButton,nil]; 

[firstButton release], [secondButton release], [flexibleSpace release]; 

이 당신에게 그런 일을 줄 것이다 도구 막대 항목이 스토리 보드에 정의되지 않고 코드를 사용하여 추가되는 경우 장치 회전.

해결 방법은 viewWillTransition 처리기를 추가하고 VC의 toolbaritems을 0으로 설정 한 다음 원하는 바 버튼 항목으로 다시 설정하는 것입니다.

// method within a UIViewController class 

// set to nil so that iOS places buttons correctly on a rotation 
tools = ... // an [UIBarButtonItem] 
toolbarItems = nil 
setToolbarItems(tools, animated: false) 

도구 모음은 [FlexibleSpace] [UIBarButtonItem] [FlexibleSpace] 인 경우 장치가 회전 할 때 다음이 처리기는 도구 모음의 중앙에 BarButtonItem을 유지하는 것입니다.

관련 문제