2016-11-10 62 views
0

我將自定義標題標籤和定製圖像添加到導航欄。我可以在我想要的位置上正確添加標題標籤,但我無法將橫向圖像添加到該標籤。我添加了一個屏幕截圖和我可以使用的代碼。使用標題標籤在導航欄中定位圖像

enter image description here enter image description here

-(void)addIconInNavBar 
{ 
    CGSize imageSize = CGSizeMake(lblHeight, lblHeight); 
    CGFloat marginX = (self.navigationController.navigationBar.frame.size.width) - (imageSize.width/2); 
    CGFloat marginY = (self.navigationController.navigationBar.frame.size.height/2) - (imageSize.height/2); 

    imageView.frame = CGRectMake(marginX, marginY , imageSize.width, imageSize.height); 
    [self.navigationController.navigationBar addSubview:imageView]; 
} 

回答

0

我認爲,更好的解決辦法是使用custom view導航欄的標題。

UIView* customViewWithImage = [self createCustomView]; 
self.navigationItem.titleView = customViewWithImage; 

你可以從title attributes財產所有權的屬性,你也可以得到appearance此信息。

0

爲的centerX請更新爲

CGFloat marginX = (self.navigationController.navigationBar.frame.size.width - imageSize.width)/2; 

或者

CGFloat marginX = (self.navigationController.navigationBar.frame.size.width/2) - (imageSize.width/2); 
+0

我想居中到自定義標題不居中導航欄。 – birdcage

0

這樣使用它,

創建自定義視圖和自定義視圖中添加標籤和圖像,並添加這個定製查看爲標題查看

UIView *vw = [[UIView alloc] initWithFrame:yourFrame]; 
UILabel *lbl = [[UILabel alloc] initWithFrame:lblFrame]; 
UIImageView *imgView = [[UIImageView alloc] initWithFrame:imgFrame]; 
[vw addSubView:lbl]; 
[vw addSubView:imgView]; 
self.navigationItem.titleView = vw;