将图像添加到 xcode 中的导航栏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13598478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:39:35  来源:igfitidea点击:

Adding image to Navigation bar in xcode?

iphonexcodeuinavigationbar

提问by Sindhia

I have added Navigation bar and Navigation Item in xib. Now I need to add image to the left of Navigation bar, but its not getting added. This is the code I'm working on:

我在 xib 中添加了导航栏和导航项。现在我需要在导航栏的左侧添加图像,但它没有被添加。这是我正在处理的代码:

- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: @"i_launcher.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
    self.navigationItem.titleView = imageView;
    [imageView release];
}

I can't understand why the image is not getting added. How can I add it?

我不明白为什么没有添加图像。我怎样才能添加它?

回答by Aron C

Looks like this is an old question with an accepted answer. However, I wanted to add:

看起来这是一个老问题,答案已被接受。但是,我想补充一点:

In at least Xcode 5, if you are using storyboardsthen you can add the image as the navigationBar's title by dragging an empty view into the title area, and then placing the ImageView inside the empty view.

至少在 Xcode 5 中,如果您使用的是故事板,那么您可以通过将一个空视图拖到标题区域中,然后将 ImageView 放置在空视图中来将图像添加为导航栏的标题。

The following screenshot should give you an idea of how it looks: Storyboard screenshot depicting imageView inside of empty view inside the navigationBar.titleView

以下屏幕截图应该让您了解它的外观: Storyboard 屏幕截图,描绘了 navigationBar.titleView 内空视图中的 imageView

回答by Paras Joshi

@implementation UINavigationBar (CustomImage) -(void)drawRect:(CGRect)rect { 

     CGRect currentRect = CGRectMake(0,0,100,45); 
     UIImage *image = [UIImage imageNamed:@"ic_launcher.png"]; 
     [image drawInRect:currentRect]; 
} 
@end 

UPDATE

更新

just add this code in your viewDidLoad:method..

只需在您的viewDidLoad:方法中添加此代码..

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourimage2.jpg"]]];    
self.navigationItem.rightBarButtonItem = item; 

and also if you want to direct set image in background of NavigationBar then use bellow line...

并且如果您想在 NavigationBar 的背景中直接设置图像,请使用波纹管...

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"i_launcher.png"] forBarMetrics:UIBarMetricsDefault];

also see this Link how-to-add-a-customized-image-button-to-uinavigationbar-in-iphone-sdk

另请参阅此链接how-to-add-a-customized-image-button-to-uinavigationbar-in-iphone-sdk

i hope this help you...

我希望这能帮助你...

回答by Bhoopendra

- (void)viewDidLoad

    {


       UIImage *image = [UIImage imageNamed:@"Your Image"];
       UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
       imageView.frame = CGRectMake(5, 10, 72, 19);
       [self.navViewController.navigationBar addSubview:imageView];
   }

回答by Jamil Hasnine Tamim

For Swift 4:

For Swift 4:

override func viewDidAppear(_ animated: Bool) {
        // 1
        let nav = self.navigationController?.navigationBar

        // 2
        nav?.barStyle = UIBarStyle.black
        nav?.tintColor = UIColor.yellow

        // 3
        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        imageView.contentMode = .scaleAspectFit

        // 4
        let image = UIImage(named: "Apple_Swift_Logo")
        imageView.image = image

        // 5
        navigationItem.titleView = imageView
    }

回答by Rajneesh071

If you want to set your image at particular point in navigation bar then you can make a view and then add your image inside the view, adjust your image frame and then add this view to your navBar.

如果你想在导航栏中的特定点设置你的图像,那么你可以创建一个视图,然后在视图中添加你的图像,调整你的图像框架,然后将此视图添加到你的导航栏中。

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Default.png"]];
[image setFrame:CGRectMake(0, 0, 44, 44)];
[view addSubview:image];
[self.navigationController.navigationBar addSubview:view];

If you are adding NavBar from xib then just make IBOutletfor your UINavigationBarconnect it in xib then

如果你是从厦门国际银行加入的NavBar则只是让IBOutletUINavigationBar连接它XIB然后

[myNavigationBar addSubview:view];

回答by Nick N

To hide / show the image between movements to another controller do this. Adjust the CGRectMake dimensions accordingly.

要隐藏/显示移动到另一个控制器之间的图像,请执行此操作。相应地调整 CGRectMake 尺寸。

Add this to your header file:

将此添加到您的头文件中:

@property (weak, nonatomic) UIImageView *navBarLogo;

Add this to your .m controller file:

将此添加到您的 .m 控制器文件中:

- (void)viewWillAppear:(BOOL)animated {

    UIImage *image = [UIImage imageNamed:@"image_name.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(53, 7, 210, 30);

    self.navBarLogo = imageView;

    [self.navigationController.navigationBar addSubview:imageView];

}

- (void)viewWillDisappear:(BOOL)animated {

    [self.navigationController.navigationBar sendSubviewToBack:self.navBarLogo];
}

回答by Shamsudheen TK

Try to set the imageView.framealso please cross check the image initailizedproperly

尝试设置imageView.frame也请交叉检查的图像initailized正确

UIImage *image = [UIImage imageNamed: @"i_launcher.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
imageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
imageView.frame.origin.y = 5.0;
self.navigationItem.titleView = imageView;
[imageView release];

回答by TieDad

Your code looks ok. But I don't know why you need to add navigation bar and item in xib. If you create your own view controller derived from UIViewController, it has contained navigation bar. You may try to remove yourself added navigation bar and item from xib and see what's happen.

你的代码看起来不错。但是我不知道为什么需要在xib中添加导航栏和项目。如果您创建自己的派生自 UIViewController 的视图控制器,则它包含导航栏。您可以尝试从 xib 中删除自己添加的导航栏和项目,看看会发生什么。

If you want to show nav bar on the first view of a view-based application, you need to modify your application class code as below:

如果你想在基于视图的应用程序的第一个视图上显示导航栏,你需要修改你的应用程序类代码,如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];

    // REMOVE THIS LINE !!!!!!!!!!!!!
    //self.window.rootViewController = self.viewController;

    // ADD BELOW TWO LINES !!!!!!!!!!!!!!!!!!!!!!!
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = nav;

    [self.window makeKeyAndVisible];
    return YES;
}

I just did a test like the following:

我刚刚做了一个如下的测试:

  1. Create a new single-view based project.
  2. Run it without any modification. It will show an empty screen.
  3. Modify didFinishLaunchingWithOptions() as above in the application.m file.
  4. In the viewcontroller.m, add the following lines.

    -(void)viewDidLoad { [super viewDidLoad]; UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; view.image = [UIImage imageNamed:@"info.png"]; self.navigationItem.titleView = view; }

  5. Now run again. You will see a nav bar with a image as title.

  1. 创建一个新的基于单视图的项目。
  2. 无需任何修改即可运行。它将显示一个空屏幕。
  3. 在 application.m 文件中像上面一样修改 didFinishLaunchingWithOptions()。
  4. 在 viewcontroller.m 中,添加以下几行。

    -(void)viewDidLoad { [super viewDidLoad]; UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)]; view.image = [UIImage imageNamed:@"info.png"]; self.navigationItem.titleView = 视图;}

  5. 现在再次运行。您将看到一个带有图像作为标题的导航栏。

回答by Vetrivel Murugan

I hope this will help you sure. if not work mean check your image name is correct.

我希望这会帮助你确定。如果不起作用意味着检查您的图像名称是否正确。

UIImage *yourimage = [UIImage imageNamed: @"i_launcher.png"];

UIButton *buttonBarButton = [ UIButton buttonWithType:UIButtonTypeCustom];
buttonBarButton.frame = CGRectMake(0, 0,yourimage.size.width,yourimage.size.height);
[buttonBarButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[buttonBarButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *buttonBarButtonItem = [[[UIBarButtonItem alloc]initWithCustomView:buttonBarButton] autorelease];
self.navigationItem.leftBarButtonItem = buttonBarButtonItem;

if you wish add action means :

如果你想添加动作意味着:

[buttonBarButton addTarget:self action:@selector(your function) forControlEvents:UIControlEventTouchUpInside];