簡體   English   中英

如何在 Objective-C 中的導航欄中心添加圖像?

[英]How to add a image at the center of navigationBar in Objective-C?

我正在 IOS 中開發,我使用以下代碼為navigationBar設置背景。

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar-back"] forBarMetrics:UIBarMetricsDefault];

我想在navigationBarnavigationBar欄的中心添加一個圖像,如下圖所示。

在此處輸入圖片說明

如何在 Objective-C 中的導航欄中心添加圖像?

提前致謝。

設置導航標題視圖如下

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img.png"]];



編輯:
如果您有大圖像,請使用以下代碼

UIImage *img = [UIImage imageNamed:@"1.png"]; UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; [imgView setImage:img]; // setContent mode aspect fit [imgView setContentMode:UIViewContentModeScaleAspectFit]; self.navigationItem.titleView = imgView;

正如你所說的為應用程序設置一個圖標。 您可能喜歡這樣做,它將顯示整個應用程序的圖標。

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon"]];
CGSize imageSize = CGSizeMake(40, 40);
CGFloat marginX = (self.navigationController.navigationBar.frame.size.width / 2) - (imageSize.width / 2);

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

輸出:

在此處輸入圖片說明

我遇到了同樣的問題,因為 iOS 11 和 Xcode 9 開始使用自動布局而不是框架來管理導航欄,你應該知道你的導航欄布局。

在 WWDC Session 204, Updating your app for iOS 11, 指出 UIToolBar 和 UINavigationBar 已升級為使用自動布局,為了避免零大小的自定義視圖,你應該了解一些關於它的事情。

UINavigation 和 UIToolBar 提供位置,不關心它。

您必須為自定義視圖的大小提供以下其中一項:

  • 寬度和高度的約束。
  • 實現內在內容大小
  • 通過約束連接你的子視圖。

回答你的問題,我用 ImageView 設置了 titleView,以兩種不同的方式提供大小:

明確給出高度和寬度。

-(void)updateNavTitleView{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[ UIImage imageNamed:@"TitleViewImage"]];
    [imageView.widthAnchor constraintEqualToConstant:160].active = YES;
    [imageView.heightAnchor constraintEqualToConstant:44].active = YES;
    self.navigationItem.titleView = imageView;
}

或者設置aspect mode為aspectFit

-(void) updateNavTitleView{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[ UIImage imageNamed:@"TitleViewImage"]];
    [imageView setContentMode:UIViewContentModeScaleAspectFit];
    self.navigationItem.titleView = imageView;
}

如果您想觀看 Session, Session 204

這里要提到的一件事是為 UIImageView 使用 3 的寬度。 這很重要 - 如果它是 2(或任何偶數),那么圖像將變得模糊。 設置 clipToBounds=NO 和 contentMode=UIViewContentModeScaleAspectFill 意味着圖像將顯示在圖像視圖的寬度之外(精細),並且將正確地按比例顯示。

UIImageView *imageView = [[UIImageView alloc]
        initWithFrame:CGRectMake(0,0,3,44)];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = NO;
    imageView.image = [UIImage imageNamed:@"navigation-logo"];
    controller.navigationItem.titleView = imageView;
 viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];

如果有人需要在 Swift (4.2) 中執行上述操作:

寫下下面的函數:

func addMiddleImage(){
    //Initialize your UIImage
    let yourImage = UIImage(named: "imageName")!.withRenderingMode(.alwaysOriginal)

    //Initialize a UIImageView
    let imageView = UIImageView(image: yourImage)

    //Set your Navigation Bar to the inialized UIImageView
    self.navigationItem.titleView = imageView

}

將其添加到您的 ViewDidLoad() 回調中:

 override func viewDidLoad() {
    super.viewDidLoad()
    self.addMiddleImage()
}

祝你好運

嘗試這個 :

 UIImage *logo = [UIImage imageNamed:@"logo.png"];
 UIImageView *imageView = [[UIImageView alloc] initWithImage: logo];
 imageView.frame = CGRectMake(160, 2, 40, 40);
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:logo];
UIImageView *navbarImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
navbarImageView.contentMode = UIViewContentModeScaleAspectFit;
self.navigationBar.titleView = navbarImageView;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM