2013-11-15 2 views
9

나는 앱을 만들고 탐색 바 위에 로고를 넣으라는 요청을 받았지만 바의 경계를 벗어나 내 시각의 일부를 덮었습니다. 이것이 정확하게 배치되는 방법입니다 (흰색 원은 로고 임).UINavigationController의 탐색 모음 위에 이미지를 추가하는 방법은 무엇입니까?

enter image description here

때문에이 응용 프로그램을 내가 UINavigationController가를 사용하기를 원하지만 조금 더 문제가 로고를 배치 할 수있는 것 같다에서 복잡한 뷰 컨트롤러 구조.

어떻게해야할까요? (이상한 배치 때문에 분명히 네비게이션 바의 제목 이미지로 로고를 넣는 것이 문제가되지 않는다.)

나는 이것을 keyWindow 나 navigationController.view의 서브 뷰로 만들 것을 고려하고 있었다. 첫 번째로 인해 내 앱이 Apple에 의해 거부 될 수 있습니까? 당신이하지 UINavigationController의 서브 클래스에서 UIViewController에서 이미지를 추가하려면

+0

이 비슷한 질문에 최근 포스트 대답 I - 나는 내 질문에 말했듯이 http://stackoverflow.com/questions/19878288/ios-custom-shape-navigation-bar – Kuba

답변

13

당신은 같은 것을 할 수 있습니다

-(void)addImageOnTopOfTheNavigationBar { 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage"]]; 

    imageView.frame = CGRectMake(....); //set the proper frame here 
    [self.navigationController.view addSubview:imageView]; 

} 
2

탐색 바는 titleView라는 것이있다. 이제이 뷰가 될 수 있습니다. 그러나 나는 그것에 단지 이미지 뷰를 넣었다.

광산이 초기화 기능에 있습니다.

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]]; 
+0

에서 봐 가지고, 이것은이다 아마 좋은 생각이 아닙니다. 주로 탐색 막대의 가운데에 맞게 크기가 조정되기 때문에 막대의 바깥 쪽에서 스트레칭해야합니다. – johnyu

+0

ah fair enough :). danypata가 말한 것처럼 하위 뷰를 추가해야합니다. 자동 크기 조정을 설정해야합니다. 그렇지 않으면 중간에 머물러 있지 않습니다. – mashdup

1
UIImage*image = [UIImage imageNamed:@"logo"]; 

float targetHeight = self.navigationController.navigationBar.frame.size.height; 
float logoRatio = image.size.width/image.size.height; 
float targetWidth = targetHeight * logoRatio; 

UIImageView*logoView = [[UIImageView alloc] initWithImage:image]; 
// X or Y position can not be manipulated because autolayout handles positions. 
//[logoView setFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width - targetWidth)/2 , (self.navigationController.navigationBar.frame.size.height - targetHeight)/2 , targetWidth, targetHeight)]; 
[logoView setFrame:CGRectMake(0, 0, targetWidth, targetHeight)]; 
self.navigationItem.titleView = logoView; 

// How much you pull out the strings and struts, with autolayout, your image will fill the width on navigation bar. So setting only height and content mode is enough/ 
[logoView setContentMode:UIViewContentModeScaleAspectFit]; 

/* Autolayout constraints also can not be manipulated since navigation bar has immutable constraints 
self.navigationItem.titleView.translatesAutoresizingMaskIntoConstraints = false; 

NSDictionary*metricsArray = @{@"width":[NSNumber numberWithFloat:targetWidth],@"height":[NSNumber numberWithFloat:targetHeight],@"margin":[NSNumber numberWithFloat:20]}; 
NSDictionary*viewsArray = @{@"titleView":self.navigationItem.titleView}; 

[self.navigationItem.titleView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(>margin=)-H:[titleView(width)]-(>margin=)-|" options:NSLayoutFormatAlignAllCenterX metrics:metricsArray views:viewsArray]]; 
[self.navigationItem.titleView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[titleView(height)]" options:0 metrics:metricsArray views:viewsArray]]; 

NSLog(@"%f", self.navigationItem.titleView.width); 
*/ 

그래서 우리가 실제로 필요로하는 모든이

UIImage*image = [UIImage imageNamed:@"logo"]; 
UIImageView*logoView = [[UIImageView alloc] initWithImage:image]; 
float targetHeight = self.navigationController.navigationBar.frame.size.height; 
[logoView setFrame:CGRectMake(0, 0, 0, targetHeight)]; 
[logoView setContentMode:UIViewContentModeScaleAspectFit]; 

self.navigationItem.titleView = logoView; 
2

당신은 쉽게 IB에서 작업을 수행 할 수 있습니다 :

  1. 컨트롤러에 이미지를 추가 (안 뷰 계층 구조에서) enter image description here enter image description here
  2. UINavigationItem의 제목보기를이 이미지에 연결하십시오. enter image description here
  3. Et voilà! enter image description here
관련 문제