2011-12-12 3 views
2

툴바를 사용하여 NavigationBar를 사용자 정의하려고합니다. 다음과 같이 내가 프로그래밍을 구현했습니다 :UIToolbar 정확한 크기

UIToolbar* tools = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 100, 44.01)]; 

을 한 후 내 내비게이션 바에 추가. 내가없는 결과는 Y와 높이 값을 변경하려고했습니다

navigationbar + toolbar

: 문제는 그 경계에이 추한 효과를 가지고있다. 이것을 피할 수있는 아이디어가 있습니까? 사전에

감사합니다, 얏사

+1

탐색 모음에 도구 모음을 추가하고 있습니까? – Till

+0

예, 일부 자습서에서이 방법을 발견했습니다 ... 잘못된 접근입니까? – yassassin

+1

예, shannogans 대답을 참조하십시오. - 이것이 "올바른"방법입니다. – Till

답변

2

부분적으로 이전 답변과 의견에 동의합니다. 제안 된 솔루션은 맞춤 검색 버튼에서 잘 작동합니다. 하지만 표준 편집 버튼을 구현하려면 어떻게해야합니까? 표준 단추/아이콘에 대한 액세스는 UIButton이 아닌 UIBarButtonItem 클래스를 통해 이루어집니다. UIView에 UIBarButtonItem 객체를 추가 할 수 없습니다.

웹에 대한 많은 연구 끝에 필자의 요구 사항을 완전히 충족하는 솔루션을 발견했습니다.

UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 95.0f, 44.01f)]; 
tools.tintColor = self.navigationController.navigationBar.tintColor; 
tools.barStyle = -1; 

을 그리고 이것은 결과 : 도구 모음은 다음과 같은 방법으로 작성해야합니다

my toolbar

는 희망이 도움이! yassa

2

나는 이런 식으로하지 않을 것입니다. navigationItem.rightBarButtonItem에 2 개의 버튼이있는보기를 추가하여 동일한 효과를 얻을 수 있습니다. 매우 간단합니다 :

// view that will hold the buttons 
UIView* container = [[UIView alloc] init]; 

// create 1 button and add it to the container 
UIButton* button = [[UIButton alloc] init........]; 
[container addSubview:button]; 


//create 2 button and add it to the container 
button = [[UIButton alloc] init.........]; 
[container addSubview:button]; 


// now create a Bar button item 
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:container]; 

// set the nav bar's right button item 
self.navigationItem.rightBarButtonItem = barButtonItem; 
+0

+1 정확하고 신뢰할 수있는 +1 처리 방법입니다. – Till

0

또는 이렇게하면됩니다. 그냥이

@interface MyToolbar : UIToolbar 

    @end 

    @implementation MyToolbar 

    - (id)initWithFrame:(CGRect)frame { 

     if ((self = [super initWithFrame:frame])) { 

      self.opaque = NO; 
      self.backgroundColor = [UIColor clearColor]; 
      self.clearsContextBeforeDrawing = YES;  
     } 
     return self; 
    } 


    - (void)drawRect:(CGRect)rect { 
     // do nothing 
    } 

    - (void)dealloc { 
     [super dealloc]; 
    } 

@end 

같은 UIToolbar의 새로운 서브 클래스를 만들고 정상 UIToolbar로 사용합니다. 이유는 모르겠지만 작동합니다.