BLOG main image
분류 전체보기 (51)
아이폰 개발 이야기 (12)
스마트폰용 홈페이지 (4)
웹표준 개발 (8)
HTML5 (2)
Objective-C (2)
뉴미디어 기획 이야기 (10)
뉴미디어 뉴스 (12)
88,774 Visitors up to today!
Today 56 hit, Yesterday 72 hit
2010/04/27 15:13

아이폰의 navigationController 의 하단 Toolbar에 이미지 버튼을 넣는 방법입니다.


먼저 팁 전체 소스입니다.

함수 예제 소스를 먼저 보시고 간단히 설명을 하도록 하겠습니다.

============================= 팁 전체소스=================================
- (void)viewWillAppear:(BOOL)animated {

  [super viewWillAppear:animated];
 
  UIToolbar *toolbar = [[UIToolbar alloc] init];
  toolbar.barStyle = UIBarStyleBlackTranslucent; //UIBarStyleBlackOpaque; //UIBarStyleBlackTranslucent;// UIBarStyleDefault;
  [toolbar sizeToFit];
 
  CGFloat toolbarHeight = [toolbar frame].size.height;
  CGRect rootViewBounds = self.parentViewController.view.bounds;
  CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
  CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
  CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
  [toolbar setFrame:rectArea];
  toolbar.tintColor = [UIColor whiteColor];
  toolbar.translucent = YES;
 
  UIImage *buttonImage = [UIImage imageNamed:@"checkmarkControllerIcon.png"];
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  [button setImage:buttonImage forState:UIControlStateNormal];
  [button addTarget:self action:@selector(info_clicked:) forControlEvents:UIControlEventTouchUpInside];
  button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
 
  UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:button];
 
 
  UIBarButtonItem *flexibleSpace1 = [UIBarButtonItem alloc];
  flexibleSpace1 = [flexibleSpace1 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
 
  UIBarButtonItem *flexibleSpace2 = [UIBarButtonItem alloc];
  flexibleSpace2 = [flexibleSpace2 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
 
  [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace1,infoButton,flexibleSpace2,nil]];
 
  toolbar.hidden = NO;
 
  [self.navigationController.view addSubview:toolbar];

  [infoButton release];
  [flexibleSpace1 release];
  [flexibleSpace2 release];
  [toolbar release];

}


- (void) info_clicked:(id)sender{

  UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:@"sss"
    message:@"버튼을 클릭하셨네요..."
    delegate:self
    cancelButtonTitle:@"닫기"
    otherButtonTitles:nil];

  [alert show];
  [alert release];
}
============================= 팁 전체소스=================================



위 소스에 대한 간단한 설명 추가합니다.

============================= 소스 설명===================================

//viewWillAppear 이벤트입니다.
- (void)viewWillAppear:(BOOL)animated {

  [super viewWillAppear:animated];  //==> 요건 이벤트 발생후 기본적으로 처리되는 부분입니다.
  

  // Navigationbar의 하단에 들어갈 Toobar를 생성합니다.
  UIToolbar *toolbar = [[UIToolbar alloc] init];
  toolbar.barStyle = UIBarStyleBlackTranslucent; //UIBarStyleBlackOpaque; //UIBarStyleBlackTranslucent;// UIBarStyleDefault;
  [toolbar sizeToFit];


  //생성될 Toolbar의 레이아웃을 설정합니다.
  CGFloat toolbarHeight = [toolbar frame].size.height;
  CGRect rootViewBounds = self.parentViewController.view.bounds;
  CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
  CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
  CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
  [toolbar setFrame:rectArea];


  //Toolbar의 색상을 설정합니다.
  toolbar.tintColor = [UIColor whiteColor];
  toolbar.translucent = YES;
 

  //Toolbar 를 생성하였으니 버튼을 넣어야 하는데요. 그 버튼을 만들기전에 버튼에 올라갈 이미지버튼을 먼저 만들어야 합니다.

  // 간단히 순서를 알아보면

  //1. 이미지를 생성한다

  //2. 버튼을 생성한다.

  //3. 버튼에 이미지를 올린다.

  //4. 생성한 버튼을 Toolbar에 올린다.

  // 이런 순서입니다.
  UIImage *buttonImage = [UIImage imageNamed:@"checkmarkControllerIcon.png"]; //=>이미지 생성
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];                         //=>버튼 생성
  [button setImage:buttonImage forState:UIControlStateNormal];

  //툴바용 버튼을 생성하고 클릭했을때 실행될 Action을 할당해줍니다. Action에 대한 구현은 아래쪽에 별도로 구현되어있습니다.
  [button addTarget:self action:@selector(info_clicked:) forControlEvents:UIControlEventTouchUpInside];
  button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
 
  UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:button];  //Toolbar버튼 생성
 
  //요기 부분의 툴바버튼의 경우는 위에서 만든 툴바버튼을 중앙에 오게 하기 위해 만들어지는 버튼입니다.

  //[infoButton]버튼의 양옆으로 하나씩 버튼을 만들어서 놓으면 [infoButton]버튼이 중앙에 오게 됩니다.
  UIBarButtonItem *flexibleSpace1 = [UIBarButtonItem alloc];
  flexibleSpace1 = [flexibleSpace1 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  UIBarButtonItem *flexibleSpace2 = [UIBarButtonItem alloc];
  flexibleSpace2 = [flexibleSpace2 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
 

  //자 버튼을 모두 만들었으니 Toolbar에 버튼을 올립니다.
  [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace1,infoButton,flexibleSpace2,nil]];
  toolbar.hidden = NO; //=>요건 툴바가 숨겨질건지 보일건지 설정한는 것입니다.
 

  // 마지막으로 Toolbar에 버튼도 올렸으니 NavigationController에 Toolbar를 올려줍니다..
  [self.navigationController.view addSubview:toolbar];


  // 모든 일은 마무리 했습니다..

  // 이제 메모리에 할당되었던 것들은 모두 지워줍니다..

  [infoButton release];
  [flexibleSpace1 release];
  [flexibleSpace2 release];
  [toolbar release];

}



// 요 Action은 위의 Toolbar버튼에 연결되는 이벤트입니다..

- (void) info_clicked:(id)sender{

  UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:@"sss"
    message:@"버튼을 클릭하셨네요..."
    delegate:self
    cancelButtonTitle:@"닫기"
    otherButtonTitles:nil];

  [alert show];
  [alert release];
}
============================= 소스 설명 ==================================


처음 개발을 시작할 때 찾기 어려운 정보라 힘들었던 부분이었습니다.
도움이 되시길 바랍니다. ^^*

Trackback Address :: http://web2log.com/trackback/27 관련글 쓰기
Name
Password
Homepage
Secret