How to set a picture programmatically in a NavBar?

后端 未结 4 555
温柔的废话
温柔的废话 2020-11-29 14:11

I have a detailview with a navigation bar with a back button and a name for the view. The navigation bar is set programmatically. The name presented is set like this.

<
4条回答
  •  再見小時候
    2020-11-29 14:51

    Add this code into viewDidLoad in your ViewController:

    UIImage *image = [UIImage imageNamed: @"myIcon.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(5, 2, 100, 39);
    [self.navigationController.navigationBar addSubview:imageView];
    

    imageView can also be declared as an instance variable, so you can access it after you've added it (e.g. if you want to remove your icon from your navigationBar at some point).

提交回复
热议问题