2013-11-15 26 views
9

Bir uygulama yapıyorum ve gezinti çubuğunun üzerinde bir logo koymak, ama aynı zamanda benim görüşümün bir kısmını kapsayan, bar sınırlarının dışına ulaşması istendi. Tam olarak yerleştirilmesi gereken budur (beyaz daire logodur).Görüntü, UINavigationController'in gezinme çubuğunun üstüne nasıl eklenir?

enter image description here

nedeniyle bu uygulamadan ben UINavigationController kullanmak istiyorum, ama biraz daha sorunlu logosu yerleştirerek yapabilir görünüyor karmaşık görünüm denetleyicileri yapısına.

Bunu yapmanın iyi bir yolu nedir? (bariz bir şekilde navigasyon çubuğunun başlık resmi olarak logoyu garip yerleştirme nedeniyle sorduğundan beri)

KeyWindow'un veya navigationController.view'in bir alt görüntüsünü yapmayı düşünüyorum. İlki benim uygulamamın Apple tarafından reddedilmesine neden olabilir mi? Eğer değil UINavigationController bir alt sınıf bir UIViewController bir görüntü eklemek istiyorsanız

+0

benzer soru üzerine son zamanlarda yazılan cevap ben - Soruma da söylediğim gibi http://stackoverflow.com/questions/19878288/ios-custom-shape-navigation-bar – Kuba

cevap

13

böyle bir şey yapabilirsiniz:

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

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

} 
2

gezinti çubuğu titleView denilen bir şey vardır. Şimdi bu herhangi bir görünüm olabilir. Ama bunun üzerine bir resim görüntüsü koydum.

Maden, init işlevindedir.

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

de bakabilirsiniz, bu Muhtemelen iyi bir fikir değil. Çoğunlukla çubuğun kenarlarının dışına uzanmaya ihtiyaç duyduğumda, gezinme çubuğunun ortasına sığacak şekilde ölçekleneceği için. – johnyu

+0

ah yeterince adil :). danypata gibi bir alt görünüm eklemeniz gerektiğini söyledi. Otomatikleştirmeyi ayarladığınızdan emin olun, aksi halde ortada kalmayacaksınız – 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); 
*/ 

Yani biz aslında gereken tüm

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

Kolayca IB halledebilirim geçerli:

  1. denetleyicinize bir görüntü ekleyin (değil görünümünüzü hiyerarşisinde) enter image description here
  2. UINavigationItem'in bu resminin başlık görünümünü bu resme bağlayabilirsiniz enter image description here
  3. Et voilà! enter image description here
İlgili konular